你好社区,我需要帮助。
我从请求中收到此信息:
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'}
我需要它看起来像这样:
const infoReduced = [{
user: 'reuse4',
BatteryWarning: 3,
GlassCracked: 5,
LCD: 2,
Microphone: 2,
VideoMicrophone: 2
}, {
user: 'reusech3',
BatteryWarning: 1,
FaceID: 2,
Microphone: 4,
VideoMicrophone: 2
}
]
我只会把“失败”加起来。 “用户”将是独一无二的。我得到了以下内容,但添加得不好,有人能告诉我为什么吗?
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)
返回以下内容:
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"
有什么可能出错的建议吗?
另一方面,有没有更简单的方法呢?谢谢!
const data = [{"StationID":"reusech3","Working":"No","Failed":"Battery Warning,","DeviceCreatedDate":"2022-07-27T18:11:45.000Z"},{"StationID":"Reuse4","Working":"No","Failed":"Battery Warning,Glass Cracked,","DeviceCreatedDate":"2022-07-27T18:05:51.000Z"},{"StationID":"reusech3","Working":"Yes","Failed":"","DeviceCreatedDate":"2022-07-27T18:01:46.000Z"},{"StationID":"Reuse4","Working":"No","Failed":"Battery Warning,","DeviceCreatedDate":"2022-07-27T17:55:56.000Z"},{"StationID":"reusech3","Working":"No","Failed":"Face ID,","DeviceCreatedDate":"2022-07-27T17:42:36.000Z"},{"StationID":"Reuse4","Working":"No","Failed":"Glass Cracked,LCD,","DeviceCreatedDate":"2022-07-27T17:03:54.000Z"},{"StationID":"reusech3","Working":"No","Failed":"Microphone,Video Microphone,","DeviceCreatedDate":"2022-07-27T17:35:57.000Z"},{"StationID":"reusech3","Working":"No","Failed":"Microphone,Video Microphone,","DeviceCreatedDate":"2022-07-27T17:25:07.000Z"},{"StationID":"Reuse4","Working":"No","Failed":"Glass Cracked,LCD,","DeviceCreatedDate":"2022-07-27T17:21:48.000Z"},{"StationID":"reusech3","Working":"No","Failed":"Face ID,Microphone,","DeviceCreatedDate":"2022-07-27T13:54:42.000Z"},{"StationID":"Reuse4","Working":"No","Failed":"Battery Warning,Glass Cracked,","DeviceCreatedDate":"2022-07-27T16:55:09.000Z"},{"StationID":"Reuse4","Working":"No","Failed":"Glass Cracked,Microphone,Video Microphone,","DeviceCreatedDate":"2022-07-27T16:46:26.000Z"},{"StationID":"Reuse4","Working":"No","Failed":"Microphone,Video Microphone,","DeviceCreatedDate":"2022-07-27T15:39:14.000Z"},{"StationID":"reusech3","Working":"No","Failed":"Microphone,","DeviceCreatedDate":"2022-07-27T13:38:59.000Z"}]
console.log(Object.values(data.reduce((a,{StationID:u,Failed},t)=>
(t=(a[u]??={user:u.toLowerCase()}),Failed.split(',').filter(i=>i.length)
.forEach(f=>(f=f.replaceAll(' ',''),t[f]??=0,t[f]++)),a),{})))