Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/435.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
javascript中具有相同键的组数组对象不起作用_Javascript_Node.js_Arrays_Ecmascript 6 - Fatal编程技术网

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

javascript中具有相同键的组数组对象不起作用,javascript,node.js,arrays,ecmascript-6,Javascript,Node.js,Arrays,Ecmascript 6,我有以下对象数组 let arr = [ { "alerts": { "bp": { "diaDiff": -20, "Notes": null, "resolveStatus": "0", "sysDiff": 10 }, "threshold": {

我有以下对象数组

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,而不是actualserid。还有,我们如何将这些数据推送到bpAlertData中。有人能帮我解决这个问题吗,因为我已经被困了很长时间了。非常感谢您的帮助。

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

设arr=[{alerts:{bp:{diaDiff:-20,Notes:null,resolveStatus:0,sysDiff:10},threshold:{diaHigh:110,diaLow:60,sysHigh:150,sysLow:90},thresholdBUnit:mmHg},measurementDate:1593934933000,actualUserID:11111},{alerts:{bp:{diaDiff:-20,注意:null,resolveStatus:0,sysDiff:10},阈值:{diaHigh:110,diaLow:60,sysHigh:150,sysLow:90},阈值BPUnit:mmHg},测量日期:1593934933000,实际值ID:2222},警报:{bp:{diaDiff:80,Notes:null,resolveststatus:0,sysDiff:20},阈值:{diaHigh:120,diaLow:60,sysHigh:140,sysLow:90},阈值bpunit:mmHg},测量日期:6593934956000,实际serid:11111},警报:{bp:{diaDiff:400,Notes:null,resolveststatus:0,sysDiff:10},阈值:{diaHigh:170,diaLow:60,sysHigh:190,sysLow:90},阈值bpunit:mmHg},测量日期:1593934944000,实际值serid:2222},警报:{bp:{diaDiff:300,Notes:null,resolveStatus:0,sysDiff:10},阈值:{diaHigh:570,diaLow:60,sysHigh:190,sysLow:90},thresholdBUnit:mmHg},measurementDate:85934989000,actualUserID:6666}, obj={success:true,data:{patient:Object.valuesarr.reducea,{alerts,measurementDate,actualUserID:userID}=>{ a[userID]| a[userID]={bpAlertData:[],userID}.bpAlertData.push{alerts,measurementDate}; 返回a; }, {}}}; console.logobj; .as console wrapper{max height:100%!important;top:0;}您可以使用函数Array.prototype.reduce进行分组,并使用函数Object.values按userID提取分组的对象

设arr=[{alerts:{bp:{diaDiff:-20,Notes:null,resolveStatus:0,sysDiff:10},threshold:{diaHigh:110,diaLow:60,sysHigh:150,sysLow:90},thresholdBUnit:mmHg},measurementDate:1593934933000,actualUserID:11111},{alerts:{bp:{diaDiff:-20,注意:null,resolveStatus:0,sysDiff:10},阈值:{diaHigh:110,diaLow:60,sysHigh:150,sysLow:90},阈值BPUnit:mmHg},测量日期:1593934933000,实际值ID:2222},警报:{bp:{diaDiff:80,Notes:null,resolveststatus:0,sysDiff:20},阈值:{diaHigh:120,diaLow:60,sysHigh:140,sysLow:90},阈值bpunit:mmHg},测量日期:6593934956000,实际serid:11111},警报:{bp:{diaDiff:400,Notes:null,resolveststatus:0,sysDiff:10},阈值:{diaHigh:170,diaLow:60,sysHigh:190,sysLow:90},阈值bpunit:mmHg},测量日期:1593934944000,实际值serid:2222},警报:{bp:{diaDiff:300,Notes:null,resolveStatus:0,sysDiff:10},阈值:{diaHigh:570,diaLow:60,sysHigh:190,sysLow:90},thresholdBUnit:mmHg},measurementDate:85934989000,actualUserID:6666}, obj={success:true,data:{patient:Object.valuesarr.reducea,{alerts,measurementDate,actualUserID:userID}=>{ a[userID]| a[userID]={bpAlertData:[],userID}.bpAlertData.push{alerts,measurementDate}; 返回a; }, {}}}; console.logobj; .作为控制台包装{最大高度:100%!重要;顶部:0;}方法 您可以按userId对元素进行分组,然后通过分组的元素进行操作

const userIdDataMapping=arr.reduceac,{actualUserID,…el}=>{ 如果acc[actualUserID]!==未定义{ acc[actualUserID].pushel }否则{ acc[actualUserID]=[el] } 返回acc }, {} const res=Object.entriesuserIdDataMapping.map[userId,bpAlertData]=>{ 用户ID, bpAlertData, } 笔记 {actualUserID,…el}从元素中排除actualUserID [userId,bpAlertData]正在销毁分配 完整代码 设arr=[ { 警报:{ B p:{ 迪亚迪夫-20, 注:空, 解决状态:0, sysDiff:10, }, 阈值:{ 迪亚高中:110, 拨号:60, 系统高:150, 西斯洛:90,, }, 阈值单位:毫米汞柱, }, 测量日期:1593934933000, 实际识别号:11111, }, { 警报:{ 英国石油公司:{ 迪亚迪夫-20, 注:空, 解决状态:0, sysDiff:10, }, 阈值:{ 迪亚高中:110, 拨号:60, 系统高:150, 西斯洛:90,, }, 阈值单位:毫米汞柱, }, 测量日期:1593934933000, 实际数字:2222, }, { 警报:{ 英国石油公司:{ diaDiff:80, 注:空, 解决状态:0, sysDiff:20, }, 阈值:{ 迪亚高中:120, 拨号:60, 系统高:140, 西斯洛:90,, }, 阈值单位:毫米汞柱, }, 测量日期:65934956000, 实际识别号:11111, }, { 警报:{ 英国石油公司:{ diaDiff:400, 注:空, 解决状态:0, sysDiff:10, }, 阈值:{ 迪亚高中:170, 拨号:60, 系统高:190, 西斯洛:90,, }, 阈值单位:毫米汞柱, }, 测量日期:1593934944000, 实际数字:2222, }, { 警报:{ 英国石油公司:{ diaDiff:300, 注:空, 解决状态:0, sysDiff:10, }, 阈值:{ 迪亚海:570, 拨号:60, 系统高:190, 西斯洛:90,, }, 阈值单位:毫米汞柱, }, 测量日期:85934989000, 实际识别号:6666, }, ] const userIdDataMapping=arr.reduceac,{actualUserID,…el}=>{ 如果acc[actualUserID]!==未定义{ acc[actualUserID].pushel }否则{ acc[actualUserID]=[el] } 返回acc }, {} const res=Object.entriesuserIdDataMapping.map[userId,bpAlertData]=>{ 用户ID, bpAlertData, } console.logresApproach 您可以按userId对元素进行分组,然后通过分组的元素进行操作

const userIdDataMapping=arr.reduceac,{actualUserID,…el}=>{ 如果acc[actualUserID]!==未定义{ acc[actualUserID].pushel }否则{ acc[actualUserID]=[el] } 返回acc }, {} const res=Object.entriesuserIdDataMapping.map[userId,bpAlertData]=>{ 用户ID, bpAlertData, } 笔记 {actualUserID,…el}从元素中排除actualUserID [userId,bpAlertData]正在销毁分配 完整代码 设arr=[ { 警报:{ 英国石油公司:{ 迪亚迪夫-20, 注:空, 解决状态:0, sysDiff:10, }, 阈值:{ 迪亚高中:110, 拨号:60, 系统高:150, 西斯洛:90,, }, 阈值单位:毫米汞柱, }, 测量日期:1593934933000, 实际识别号:11111, }, { 警报:{ 英国石油公司:{ 迪亚迪夫-20, 注:空, 解决状态:0, sysDiff:10, }, 阈值:{ 迪亚高中:110, 拨号:60, 系统高:150, 西斯洛:90,, }, 阈值单位:毫米汞柱, }, 测量日期:1593934933000, 实际数字:2222, }, { 警报:{ 英国石油公司:{ diaDiff:80, 注:空, 解决状态:0, sysDiff:20, }, 阈值:{ 迪亚高中:120, 拨号:60, 系统高:140, 西斯洛:90,, }, 阈值单位:毫米汞柱, }, 测量日期:65934956000, 实际识别号:11111, }, { 警报:{ 英国石油公司:{ diaDiff:400, 注:空, 解决状态:0, sysDiff:10, }, 阈值:{ 迪亚高中:170, 拨号:60, 系统高:190, 西斯洛:90,, }, 阈值单位:毫米汞柱, }, 测量日期:1593934944000, 实际数字:2222, }, { 警报:{ 英国石油公司:{ diaDiff:300, 注:空, 解决状态:0, sysDiff:10, }, 阈值:{ 迪亚海:570, 拨号:60, 系统高:190, 西斯洛:90,, }, 阈值单位:毫米汞柱, }, 测量日期:85934989000, 实际识别号:6666, }, ] const userIdDataMapping=arr.reduceac,{actualUserID,…el}=>{ 如果acc[actualUserID]!==未定义{ acc[actualUserID].pushel }否则{ acc[actualUserID]=[el] } 返回acc }, {} const res=Object.entriesuserIdDataMapping.map[userId,bpAlertData]=>{ 用户ID, bpAlertData, }
console.logres您可以使用id进行分组,并获得一组患者

常数 数据=[{alerts:{bp:{diaDiff:-20,注意:null,resolveStatus:0,sysDiff:10},阈值:{diaHigh:110,diaLow:60,sysHigh:150,sysLow:90},thresholdBUnit:mmHg},measurementDate:1593934933000,actualUserID:11111},注意:null,resolveStatus:0,sysDiff:10},阈值:{diaHigh:110,diaLow:60,sysHigh:150 ,sysLow:90},thresholdBpUnit:mmHg},measurementDate:1593934933000,ActualsErid:2222},{alerts:{bp:{diaDiff:80,Notes:null,resolveStatus:0,sysDiff:20},thresholdBpUnit:20},thresholdBpUnit:60,sysLow:140,sysLow:90},thresholdBpUnit:mmHg},measurementDate:6593934956000,ActualsErid:11111},{alerts:{bp:{diaDiff:400,Notes:null,resolveStatus:0,sysDiff:10},阈值:{diaHigh:170,diaLow:60,sysHigh:190,sysLow:90},thresholdBUnit:mmHg},measurementDate:1593934944000,actualUserID:2222},警报:{bp:{diaDiff:300,Notes:null,resolveStatus:0,sysDiff:10},阈值:{diaHigh:570,diaLow:60,sysHigh:190,sysLow:90},thresholdBpUnit:mmHg},measurementDate:8593934989000,actualUserID:6666}, patient=Object.valuesdata.reducer,{actualserid:userID,…o}=>{ 如果!r[userID]r[userID]={userID,bpAlertData:[]}; r[userID].bpAlertData.pusho; 返回r; }, [], 响应={success:true,数据:{patient}}; console.logresponse;
.as console wrapper{max height:100%!important;top:0;}您可以使用id分组并获得患者数组

常数 数据=[{alerts:{bp:{diaDiff:-20,注意:null,resolveStatus:0,sysDiff:10},阈值:{diaHigh:110,diaLow:60,sysHigh:150,sysLow:90},thresholdBUnit:mmHg},measurementDate:1593934933000,actualUserID:11111},注意:null,resolveStatus:0,sysDiff:10},阈值:{diaHigh:110,diaLow:60,sysHigh:150,sysLow:90},thresholdBpUnit:mmHg},measurementDate:1593934933000,actualUserID:2222},{alerts:{bp:{diadidiff:80,Notes:null,resolveStatus:0,sysDiff:20},阈值:{diaHigh:120,diaLow:60,sysHigh:140,sysLow:90},thresholdBpUnit:mmHg},measurementDate:6593934956000,actualUserID:11111},{警报:{bp:{diaDiff:400,注意:null,resolveStatus:0,sysDiff:10},阈值:{diaHigh:170,diaLow:60,sysHigh:190,sysLow:90},阈值BPUnit:mmHg},measurementDate:1593934944000,actualUserID:2222},{警报:{bp:{diaDiff:300,Notes:null,resolveStatus:0,sysDiff:10},阈值:{diaHigh:570,diaLow:60,sysHigh:190,sysLow:90},thresholdBUnit:mmHg},measurementDate:85934989000,actualUserID:6666}, patient=Object.valuesdata.reducer,{actualserid:userID,…o}=>{ 如果!r[userID]r[userID]={userID,bpAlertData:[]}; r[userID].bpAlertData.pusho; 返回r; }, [], 响应={success:true,数据:{patient}}; console.logresponse;
.作为控制台包装{最大高度:100%!重要;顶部:0;}我不希望结果中包含actualUserID。那么我们如何才能使其更好?您能告诉我actualUserID:userID的用途吗?它是对已解构属性的重命名:我不希望结果中包含actualUserID。那么我们如何才能使其更好?您能让我知道actualUserID:userID的用途吗?它是对de的重命名结构化属性:你能解释一下其他部分吗?acc[actualUserID]=[el]我没有理解关于[]@毗湿奴它正在用一个元素初始化一个数组,这里是el,你能给我一些关于使用它的参考吗?我仍然对Square括号表示法感到困惑:@Vishnu:它只是数组,你能解释一下其他部分吗?acc[actualUserID]=[el]我没有理解关于[]@毗瑟奴它正在用一个元素初始化一个数组,这是El,你能给我一些关于使用它的参考吗?我仍然对sqaure括号表示法感到困惑:@Vishnu:它只是数组。你能告诉我为什么我们在reduce方法中使用actualUserID:userID吗?我没有得到答案point@Vishnu这就是所谓的分解赋值,即使用这种方式是因为a希望创建具有所需属性名称的对象。您能告诉我为什么在reduce方法中使用actualserid:userID吗?我没有得到point@Vishnu这就是所谓的分解分配,我使用这种方式是因为a希望创建具有所需属性名称的对象。