简体   繁体   English

Javascript - 比较两个数组对象和 append 值

[英]Javascript - Compare two array objects and append values

How to compare two array objects and append first array object on match criteria key with second?如何比较两个数组对象和 append 第一个数组 object 与第二个匹配条件键? Pls suggest.请建议。

For example, in the below data, I need to compare vale_data service key with sli_data service key and if it matches, then append sli_data vol_sli key value to vale_data .例如,在下面的数据中,我需要将vale_data服务密钥与sli_data服务密钥进行比较,如果匹配,则将 append sli_data vol_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"}]

I have tried like below, but it didn't worked.我已经尝试如下,但没有奏效。

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'] })
      }
  }
}

Expected output:预期 output:

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: ""} ]

Actual output:实际 output:

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

You can try this, by creating an object as a map and then using that to get the output array.您可以尝试这个,通过创建 object 作为 map 然后使用它来获取 output 数组。 Uses the Javascript MAP to store the results使用Javascript MAP存储结果

 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 sli_dataMap = new Map(sli_data.map(({service,vol_sli}) => ([service,vol_sli]))); vale_data = vale_data.map(obj => ({...obj,vol_sli:sli_dataMap.get(obj.service) || ''})); console.log(vale_data);

Iterate through each array and if one sli.service is the same as one vale.service add a vol_sli field equal to sli.vol_sli to this vale.遍历每个数组,如果一个 sli.service 与一个 vale.service 相同,则向该 vale 添加一个等于 sli.vol_sli 的 vol_sli 字段。

   vale_data.forEach(function (vale) {
        sli_data.forEach(function (sli) {
            if (sli.service === vale.service) {
                vale.vol_sli = sli.vol_sli;
            }
        })
    });
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 result = vale_data.map(vale_data_value => {
        const check_sli_data = sli_data.find(sli_data_value => sli_data_value.service === vale_data_value.service );
        if(check_sli_data){
          vale_data_value.vol_sli = check_sli_data.vol_sli
        }
        return vale_data_value;
   });

   console.log(result)       

 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"}]; vale_data.map(vale_record => { let rec_found = sli_data.find(sli_record => sli_record.service === vale_record.service); if(rec_found) { vale_record['vol_sli'] = rec_found['vol_sli']; } else { vale_record['vol_sli'] = '' } return vale_record; }); console.log(vale_data);

One of the possible ways of accomplishing this is using map and find the following way:实现此目的的一种可能方法是使用mapfind以下方法:

 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 result = vale_data.map(element => { let sli = sli_data.find(service => service["service"] === element["service"]) || { vol_sli: "" }; return {...element, ...sli }; }); console.log(result);

Slight variation on some of the other answers:其他一些答案略有不同:

  1. Combine all values into a single array,将所有值组合到一个数组中,
  2. reduce the array into a Map将数组reduceMap

 const vale_data = [ {service: "allocateorder", vol_slo: "10000"}, {service: "cancelorder", vol_slo: "10000"}, {service: "adviceorder", vol_slo: "10000"}, ] const sli_data = [ {service: "allocateorder", vol_sli: "0.9484949"}, {service: "cancelorder", vol_sli: "0.242322"}, ] const mergedData = [...vale_data, ...sli_data].reduce((map, item) => { const existing = map.get(item.service) || { vol_slo: "", vol_sli: "" }; map.set(item.service, {...existing, ...item, }) return map; }, new Map()); console.log(Array.from(mergedData.values()));

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

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