繁体   English   中英

javascript中具有相同键的组数组对象不起作用

[英]Group array objects with same key in javascript is not working

我有以下对象数组。

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);

我希望响应中包含 userID,而不是实际用户 ID。 还有我们如何将这些数据推送到 bpAlertData 中。 所以有人可以帮我解决这个问题,因为我已经坚持了很长时间。 任何帮助将非常感激。

您可以使用函数Array.prototype.reduce进行分组,使用函数Object.values提取按userID分组的对象。

 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" } ], obj = { "success": true, "data": {"patient": Object.values(arr.reduce((a, {alerts, measurementDate, actualUserID: userID}) => { (a[userID] || (a[userID] = {bpAlertData: [], userID})).bpAlertData.push({alerts, measurementDate}); return a; }, {}))}}; console.log(obj);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

方法

您可以按userId元素进行分组,然后通过该分组进行操作

const userIdDataMapping = arr.reduce((acc, { actualUserID, ...el }) => {
  if (acc[actualUserID] !== undefined) {
    acc[actualUserID].push(el)
  } else {
    acc[actualUserID] = [el]
  }
  return acc
}, {})

const res = Object.entries(userIdDataMapping).map(([userId, bpAlertData]) => ({
  userId,
  bpAlertData,
}))

笔记

  • { actualUserID, ...el }从元素中排除actualUserId
  • [userId, bpAlertData]破坏赋值

完整代码

 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", }, ] const userIdDataMapping = arr.reduce((acc, { actualUserID, ...el }) => { if (acc[actualUserID] !== undefined) { acc[actualUserID].push(el) } else { acc[actualUserID] = [el] } return acc }, {}) const res = Object.entries(userIdDataMapping).map(([userId, bpAlertData]) => ({ userId, bpAlertData, })) console.log(res)


参考

Object.entries()

破坏赋值

展开语法 (...)

您可以使用 id 分组并获得一组患者。

 const data = [{ 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" }], patient = Object.values(data.reduce((r, { actualUserID: userID, ...o }) => { if (!r[userID]) r[userID] = { userID, bpAlertData: [] }; r[userID].bpAlertData.push(o); return r; }, [])), response = { success: true, data: { patient } }; console.log(response);
 .as-console-wrapper { max-height: 100% !important; top: 0; }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM