Javascript - 比较两个数组对象并附加值

如何比较两个数组对象并在匹配条件键上附加第一个数组对象和第二个?请建议。


例如,在下面的数据中,我需要将vale_data服务密钥与sli_data服务密钥进行比较,如果匹配,则将sli_datavol_sli 密钥值附加到vale_data.


var vale_data = [

          {service: "allocateorder", vol_slo: "10000"},

          {service: "cancelorder", vol_slo: "10000"},

          {service: "adviceorder", vol_slo: "10000"}]


var sli_data = [

          {service: "allocateorder", vol_sli: "0.9484949"},

          {service: "cancelorder", vol_sli: "0.242322"}]

我已经尝试过如下所示,但没有奏效。


var vale_data = [

          {service: "allocateorder", vol_slo: "10000"},

          {service: "cancelorder", vol_slo: "10000"},

          {service: "adviceorder", vol_slo: "10000"}]


var sli_data = [

          {service: "allocateorder", vol_sli: "0.9484949"},

          {service: "cancelorder", vol_sli: "0.242322"}]


for (i=0; i<Object.keys(vale_data).length; i++) {

  for (j=0; j<Object.keys(sli_data).length; j++) {

      if (Object.values(vale_data)[i]['service'] == Object.values(sli_data)[i]['service']) {

          vale_data.map(item => {item.vol_sli = Object.values(sli_data)[j]['vol_sli'] })

      }

  }

}

预期输出:


vale_data = [

 {service: "allocateorder", vol_slo: "10000", vol_sli: "0.9484949"},

 {service: "cancelorder", vol_slo: "10000", vol_sli: "0.242322"},

 {service: "adviceorder", vol_slo: "10000", vol_sli: ""} ]

实际输出:


error: Uncaught TypeError: Cannot read property 'service' of undefined


MM们
浏览 265回答 3
3回答

拉莫斯之舞

您可以尝试这样做,方法是创建一个对象作为映射,然后使用它来获取输出数组。使用Javascript MAP存储结果var vale_data = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {service: "allocateorder", vol_slo: "10000"},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {service: "cancelorder", vol_slo: "10000"},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {service: "adviceorder", vol_slo: "10000"}]var sli_data = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {service: "allocateorder", vol_sli: "0.9484949"},&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {service: "cancelorder", vol_sli: "0.242322"}]var sli_dataMap = new Map(sli_data.map(({service,vol_sli}) => ([service,vol_sli])));&nbsp;vale_data = vale_data.map(obj => ({...obj,vol_sli:sli_dataMap.get(obj.service) || ''}));console.log(vale_data);

HUH函数

var vale_data = [&nbsp; &nbsp; {service: "allocateorder", vol_slo: "10000"},&nbsp; &nbsp; {service: "cancelorder", vol_slo: "10000"},&nbsp; &nbsp; {service: "adviceorder", vol_slo: "10000"}];var sli_data = [&nbsp; &nbsp; {service: "allocateorder", vol_sli: "0.9484949"},&nbsp; &nbsp; {service: "cancelorder", vol_sli: "0.242322"}];vale_data.map(vale_record => {&nbsp; &nbsp; let rec_found = sli_data.find(sli_record => sli_record.service === vale_record.service);&nbsp; &nbsp; if(rec_found) {&nbsp; &nbsp; vale_record['vol_sli'] = rec_found['vol_sli'];&nbsp; &nbsp; }&nbsp; &nbsp; else {&nbsp; &nbsp; vale_record['vol_sli'] = ''&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; return vale_record;});console.log(vale_data);

潇潇雨雨

实现这一点的可能方法之一是使用map和find以下方式:var vale_data = [{&nbsp; &nbsp; service: "allocateorder",&nbsp; &nbsp; vol_slo: "10000"&nbsp; },&nbsp; {&nbsp; &nbsp; service: "cancelorder",&nbsp; &nbsp; vol_slo: "10000"&nbsp; },&nbsp; {&nbsp; &nbsp; service: "adviceorder",&nbsp; &nbsp; vol_slo: "10000"&nbsp; }];var sli_data = [{&nbsp; &nbsp; service: "allocateorder",&nbsp; &nbsp; vol_sli: "0.9484949"&nbsp; },&nbsp; {&nbsp; &nbsp; service: "cancelorder",&nbsp; &nbsp; vol_sli: "0.242322"&nbsp; }];var result = vale_data.map(element => {&nbsp; let sli = sli_data.find(service => service["service"] === element["service"]) ||&nbsp;&nbsp; &nbsp; { vol_sli: "" };&nbsp; return { ...element,&nbsp; &nbsp; ...sli&nbsp; };});console.log(result);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript