【发布时间】:2020-12-16 10:52:28
【问题描述】:
我有以下对象数组。
let arr = [
{
"alerts": {
"bp": {
"diaDiff": -20,
"Notes": null,
"resolveStatus": "0",
"sysDiff": 10
},
"threshold": {
"diaHigh": "110",
"diaLow": "60",
"sysHigh": "150",
"sysLow": "90"
},
"thresholdBpUnit": "mmHg"
},
"measurementDate": 1593934933000,
"actualUserID ": "11111"
},
{
"alerts": {
"bp": {
"diaDiff": -20,
"Notes": null,
"resolveStatus": "0",
"sysDiff": 10
},
"threshold": {
"diaHigh": "110",
"diaLow": "60",
"sysHigh": "150",
"sysLow": "90"
},
"thresholdBpUnit": "mmHg"
},
"measurementDate": 1593934933000,
"actualUserID ": "2222"
},
{
"alerts": {
"bp": {
"diaDiff": 80,
"Notes": null,
"resolveStatus": "0",
"sysDiff": 20
},
"threshold": {
"diaHigh": "120",
"diaLow": "60",
"sysHigh": "140",
"sysLow": "90"
},
"thresholdBpUnit": "mmHg"
},
"measurementDate": 6593934956000,
"actualUserID ": "11111"
},
{
"alerts": {
"bp": {
"diaDiff": 400,
"Notes": null,
"resolveStatus": "0",
"sysDiff": 10
},
"threshold": {
"diaHigh": "170",
"diaLow": "60",
"sysHigh": "190",
"sysLow": "90"
},
"thresholdBpUnit": "mmHg"
},
"measurementDate": 1593934944000,
"actualUserID ": "2222"
},
{
"alerts": {
"bp": {
"diaDiff": 300,
"Notes": null,
"resolveStatus": "0",
"sysDiff": 10
},
"threshold": {
"diaHigh": "570",
"diaLow": "60",
"sysHigh": "190",
"sysLow": "90"
},
"thresholdBpUnit": "mmHg"
},
"measurementDate": 8593934989000,
"actualUserID ": "6666"
}
];
我需要合并具有相同 userID 键的数组对象,并期待以下输出。
let response = {
"success": true,
"data": {
"patient": [
{
"userID": "11111", // I need userID not actualUserID
"bpAlertData": [
{
alerts: { // object },
measurementDate: 1593934933000
},
{
alerts: { // object },
measurementDate: 6593934956000
}
]
},
{
"userID": "22222",
"bpAlertData": [
{
alerts: { // object },
measurementDate: 1593934944000
},
{
alerts: { // object },
measurementDate: 1593934933000
}
]
}
]
},
};
我尝试了以下方法,但还是坚持了下来。
arr.forEach((item) => {
let filteredData = response.data.patient.filter(patient => patient.userID === item.actualUserID);
if(filteredData.length) {
const existingIndex = response.data.patient.indexOf(filteredData[0]);
response.data.patient[existingIndex].bpAlertData = response.data.patient[existingIndex].bpAlertData.concat(item);
} else {
response.data.patient.push(item);
}
});
console.log(response.data.patient);
我期望响应中的不是实际用户 ID,而是用户 ID。此外,我们如何将这些数据推送到 bpAlertData。所以有人可以帮我解决这个问题,因为我已经坚持了很长时间。任何帮助将不胜感激。
【问题讨论】:
标签: javascript node.js arrays ecmascript-6