Hello community, I need help.
I am receiving this information from the request:
0: {StationID: 'reusech3', Working: 'No', Failed: 'Battery Warning,', DeviceCreatedDate: '2022-07-27T18:11:45.000Z'}
1: {StationID: 'Reuse4', Working: 'No', Failed: 'Battery Warning,Glass Cracked,', DeviceCreatedDate: '2022-07-27T18:05:51.000Z'}
2: {StationID: 'reusech3', Working: 'Yes', Failed: '', DeviceCreatedDate: '2022-07-27T18:01:46.000Z'}
3: {StationID: 'Reuse4', Working: 'No', Failed: 'Battery Warning,', DeviceCreatedDate: '2022-07-27T17:55:56.000Z'}
4: {StationID: 'reusech3', Working: 'No', Failed: 'Face ID,', DeviceCreatedDate: '2022-07-27T17:42:36.000Z'}
5: {StationID: 'Reuse4', Working: 'No', Failed: 'Glass Cracked,LCD,', DeviceCreatedDate: '2022-07-27T17:03:54.000Z'}
6: {StationID: 'reusech3', Working: 'No', Failed: 'Microphone,Video Microphone,', DeviceCreatedDate: '2022-07-27T17:35:57.000Z'}
7: {StationID: 'reusech3', Working: 'No', Failed: 'Microphone,Video Microphone,', DeviceCreatedDate: '2022-07-27T17:25:07.000Z'}
8: {StationID: 'Reuse4', Working: 'No', Failed: 'Glass Cracked,LCD,', DeviceCreatedDate: '2022-07-27T17:21:48.000Z'}
9: {StationID: 'reusech3', Working: 'No', Failed: 'Face ID,Microphone,', DeviceCreatedDate: '2022-07-27T13:54:42.000Z'}
10: {StationID: 'Reuse4', Working: 'No', Failed: 'Battery Warning,Glass Cracked,', DeviceCreatedDate: '2022-07-27T16:55:09.000Z'}
11: {StationID: 'Reuse4', Working: 'No', Failed: 'Glass Cracked,Microphone,Video Microphone,', DeviceCreatedDate: '2022-07-27T16:46:26.000Z'}
12: {StationID: 'Reuse4', Working: 'No', Failed: 'Microphone,Video Microphone,', DeviceCreatedDate: '2022-07-27T15:39:14.000Z'}
13: {StationID: 'reusech3', Working: 'No', Failed: 'Microphone,', DeviceCreatedDate: '2022-07-27T13:38:59.000Z'}
I need it to look something similar to this:
const infoReduced = [{
user: 'reuse4',
BatteryWarning: 3,
GlassCracked: 5,
LCD: 2,
Microphone: 2,
VideoMicrophone: 2
}, {
user: 'reusech3',
BatteryWarning: 1,
FaceID: 2,
Microphone: 4,
VideoMicrophone: 2
}
]
I would only add up the "Failed". The "user" would be unique. I got to the following, but it is not adding well, could someone tell me why?
const infoReduced = info.reduce((acc, ele) => {
if (ele.Failed !== '') {
const failures = ele.Failed.split(',').filter(ele => ele !== '')
const properties = {}
failures.forEach(f => {
return (
properties[f] = 1
)
})
const obj = acc.find(ob => ob.user === ele.StationID)
obj
? Object.entries(properties).forEach(([key, value]) => {
Object.entries(obj).forEach(([k, v]) => {
if (key === k) {
console.log('KEY PROPERTIES', key)
console.log('KEY OBJ', k)
obj[k]++
} else {
obj[key] = 1
}
})
})
: acc.push({
user: ele.StationID,
...properties
})
}
return acc
}, [])
console.log(infoReduced)
Return the following:
0: Battery Warning: 1
Face ID: 1
Microphone: 1
Video Microphone: 2
user: "reusech3"
1: Battery Warning: 1
Glass Cracked: 1
LCD: 2
Microphone: 1
Video Microphone: 2
user: "Reuse4"
Any suggestions on what could be wrong?
On the other hand, is there a simpler way to do it? THANK YOU!