3

我想使用另一个 JS 数组替换 JS 数组属性值。我已经解释如下。

const arr1 = [
{
  code: "XXY",
  dis: "cont1",
  note: "Note for cont1"
}, 
{
  code: "AAW",
  dis: "cont2",
  note: "Note for cont2"
}, 
{
  code: "TTR",
  dis: "cont5",
  note: "Note for cont5"
}, 
{
  code: "MMN",
  dis: "cont10",
  note: "Note for cont10"
}]
const new_array = [
  {
    "code": "AAW",
    "dis": "cont2 new",
    "note": "Note for cont2"
  },
  {
    "code": "TTR",
    "dis": "cont5",
    "note": "New Note for cont5"
  }]

预期输出:

[
 {
  code: "XXY",
  dis: "cont1",
  note: "Note for cont1"
 }, 
 {
  code: "AAW",
  dis: "cont2 new",
  note: "Note for cont2"
 }, 
 {
  code: "TTR",
  dis: "cont5",
  note: "New Note for cont5"
 }, 
 {
  code: "MMN",
  dis: "cont10",
  note: "Note for cont10"
 }
]

我们需要检查所有arr1等于和比较属性new_arr.code的元素。如果不等于,则value 应替换为。这对财产也是一样的。arr1.codedisnotearr1.disnew_arr.disarr1.disnew_arr.disnote

试过的代码:

arr1.forEach(function(item1) {
        var item2 = arr1.find(function (item2) {
            return arr1.code === new_array.code;
        });
    })

console.log(arr1);

电流输出:

[
  {
    "code": "XXY",
    "dis": "cont1",
    "note": "Note for cont1"
  },
  {
    "code": "AAW",
    "dis": "cont2",
    "note": "Note for cont2"
  },
  {
    "code": "TTR",
    "dis": "cont5",
    "note": "Note for cont5"
  },
  {
    "code": "MMN",
    "dis": "cont10",
    "note": "Note for cont10"
  }
]

我该如何解决这个问题?

4

4 回答 4

3
new_array.forEach(o1 => { // for every replacement object
  // find the corresponding object in the original array
  const found = arr1.find(o2 => o2.code === o1.code); 
  // if there is a corresponding object
  if (found) {
    // copy its properties over
    found.dis = o1.dis;
    found.note = o1.note;
  }
})

请注意,这具有较差的O(n^2)时间复杂度,如果您有一个大型数据集,请考虑使用其他答案所建议的更快查找的集合。

于 2020-10-28T13:31:15.050 回答
3

给定一个可以合并原始项目的方法,并带有一组可能的更新:

const mergeItem = (item, arr, finder) =>{
  var other = arr.find(x => finder(item,x));
  if(other != null){
    Object.entries(other).forEach(([key,value]) => item[key] = value);
  }
  return item;
}

合并 2 个数组的代码相当简单

var result = arr1.map(item => mergeItem(item,new_array, (x,y) => x.code == y.code));

现场示例:

const arr1 = [{"code":"XXY","dis":"cont1","note":"Note for cont1"},{"code":"AAW","dis":"cont2","note":"Note for cont2"},{"code":"TTR","dis":"cont5","note":"Note for cont5"},{"code":"MMN","dis":"cont10","note":"Note for cont10"}];
const new_array = [{"code":"AAW","dis":"cont2 new","note":"Note for cont2"},{"code":"TTR","dis":"cont5","note":"New Note for cont5"}];
  
  
const mergeItem = (item, arr, finder) =>{
  var other = arr.find(x => finder(item,x));
  if(other != null){
    Object.entries(other).forEach(([key,value]) => item[key] = value);
  }
  return item;
}

var result = arr1.map(item => mergeItem(item,new_array, (x,y) => x.code == y.code));

console.log(result);

于 2020-10-28T13:36:16.120 回答
3
const mappedArray = arr1.map((x) => {
  const filteredValue = new_array.filter((y) => y.code === x.code);
  return filteredValue.length > 0 ? filteredValue[0] : x;
});
于 2020-10-28T13:52:03.830 回答
3

使用第二个数组创建一个对象,其中代码是在 O(1) 时间内查找元素的关键因此,复杂性降低到 O(n) 时间。

const arr1 = [{
    code: "XXY",
    dis: "cont1",
    note: "Note for cont1"
  },
  {
    code: "AAW",
    dis: "cont2",
    note: "Note for cont2"
  },
  {
    code: "TTR",
    dis: "cont5",
    note: "Note for cont5"
  },
  {
    code: "MMN",
    dis: "cont10",
    note: "Note for cont10"
  }
]

const new_array = [{
    "code": "AAW",
    "dis": "cont2 new",
    "note": "Note for cont2"
  },
  {
    "code": "TTR",
    "dis": "cont5",
    "note": "New Note for cont5"
  }
]

const t = new_array.reduce((res, item) => {
  res[item.code] = item
  return res;
}, {})

arr1.forEach(item => {
  const found = t[item.code]
  if (found) {
    item.dis = found.dis
    item.note = found.note
  }
})
console.log(arr1)

于 2020-10-28T13:54:13.893 回答