将数据从一个阵列复制到另一阵列

时间:2018-10-03 04:05:42

标签: javascript arrays json reactjs

数组一:

 array1 = [{
        "id": 1,
        "name": "aaaaa",
        "attr": [{"attr_code": "a_id", "value": "5"}]
      },
      {
        "id": 2,
        "name": "bbbbb",
        "attr": [{"attr": "a_id", "value": "4"}]
      }]

第二个数组:

array2 = [{
    "id": 4,
    "name": "bef",
  },
  {
    "id": 5,
    "name": "bcd",
  }]

结果数组:

 resultingArray =  [{
    "id": 1,
    "name": "aaaaa",
    "attr": [{"attr_code": "a_id", "value": "5"}],
    "a_id" :   {"id": 5, "name": "bcd"}
  },
    {
      "id": 2,
      "name": "bbbbb",
      "attr": [{"attr": "a_id", "value": "4"}],
      "a_id" : {"id": 4, "name": "bef"}
    }]

我正在基于array2的ID将array2对象添加到array1中。我尝试在两个数组上使用map函数进行比较并添加对象,但未成功。你能建议我怎么做吗?

谢谢

5 个答案:

答案 0 :(得分:1)

根据array2的ID将array2对象添加到array1中。

let array1 = 
[
  {
    "id": 1,
    "name": "aaaaa",
    "attr": [{"attr_code": "a_id", "value": "5"}]
  },
  {
    "id": 2,
    "name": "bbbbb",
    "attr": [{"attr": "a_id", "value": "4"}]
  }
];
      
let array2 = [{
    "id": 4,
    "name": "bef",
  },
  {
    "id": 5,
    "name": "bcd",
  }
];
 
let resultingArray=[]; 

array1.forEach(function(element) {
  element['a_id'] = [];
  element['attr'].forEach(function(attr) {
        element['a_id'].push(array2.find(function(item) {
        return item.id == attr.value;
      }));
   });
  resultingArray.push(element)
});

console.log(resultingArray);

答案 1 :(得分:0)

我假设您打算在value的每个对象中提取ID等于array1字段的对象。

var array1 = [{
        "id": 1,
        "name": "aaaaa",
        "attr": [{"attr_code": "a_id", "value": "5"}]
      },
      {
        "id": 2,
        "name": "bbbbb",
        "attr": [{"attr": "a_id", "value": "4"}]
      }];

var array2 = [{
    "id": 4,
    "name": "bef",
  },
  {
    "id": 5,
    "name": "bcd",
  }];

var resultingArray = [];
for(var i = 0; i < array1.length; i++) {
    resultingArray[i] = array1[i];
    for(var j = 0; j < array2.length; j++) {
        if(resultingArray[i].attr[0].attr_code.value === array2[j].id) {
            resultingArray[i].push("a_id": array2[j]);
        }
    }
} 

您只需要浏览array1,对于array1中的每个对象,您需要在array2中找到与标准匹配的对应对象。

答案 2 :(得分:0)

您可以使用数组映射和数组索引来执行以下操作:

var array1 = [{
        "id": 1,
        "name": "aaaaa",
        "attr": [{"attr_code": "a_id", "value": "5"}]
      },
      {
        "id": 2,
        "name": "bbbbb",
        "attr": [{"attr": "a_id", "value": "4"}]
      }];
var array2 = [{
    "id": 4,
    "name": "bef",
  },
  {
    "id": 5,
    "name": "bcd",
  }];
var result = array1.map(current=>{
  //find index of attr in array2
  let index = array2.findIndex(c=>{
    if (c['id']===(Number(current['attr'][0]['value'])))
      return c;
  });
  current["a_id"] = array2[index];
  return current;
});
console.log(result);

答案 3 :(得分:0)

请检查以下代码是否满足您的要求。您可能需要进行一些更改。

function mergeArrays3 (arr1, arr2) {
 return arr1.map((value, index) => {
  let object = null;
  let result = {...value};
  for (let element of arr2) {
   if (element.id == parseInt(value.attr[0].value)) {
    object = element;
    break;
   }
  }
  if (object != null) {
   let attr = value.attr[0];
   if (attr.hasOwnProperty("attr")) {
    result[value.attr[0].attr] = object;
   } else if (attr.hasOwnProperty("attr_code")) {
    result[value.attr[0].attr_code] = object;
   }
  }
  return result;
 });
}

我遍历第一个数组,然后在第二个数组中找到一个与id.value.attr [0] .value相匹配的元素。如果找到,那么我将该对象添加到value.attr [0] .attr或value.attr [0] .attr_code的键的第一个数组中。

答案 4 :(得分:0)

  

我尝试在两个数组上使用map函数进行比较和添加   对象,但我没有成功

下面是使用map()的功能编程方法:

/* GIVEN */
const array1 = [{
    "id": 1,
    "name": "aaaaa",
    "attr": [{
      "attr_code": "a_id",
      "value": "5"
    }]
  },
  {
    "id": 2,
    "name": "bbbbb",
    "attr": [{
      "attr": "a_id",
      "value": "4"
    }]
  }
]

const array2 = [{
    "id": 4,
    "name": "bef",
  },
  {
    "id": 5,
    "name": "bcd",
  }]

/* From array2, make an object keyed by the 'id' field. We'll use this as a key-value lookup table  */
const lookupTable = array2.reduce((accum, item) => {
    accum[item.id.toString()] = item
    return accum
  }, {})
  
  
console.log('***LOOKUP TABLE***\n', lookupTable) // result is an object we use to lookup

/* From array1, we append data from the lookup table */
const final = array1.map(item => {
    item.a_id = lookupTable[item.attr[0].value]
    return item
  })

console.log("***RESULT***\n", final)

希望这会有所帮助。 干杯,