繁体   English   中英

如何根据匹配索引上的另一个数组更新数组?

[英]How can I update an array based on another array on matching index?

我有以下两个对象数组。 我想更新 array1 元素以反映 transid 匹配的 array2 元素。 所以我的结果应该是一个新数组,其中更新了 array1 中的两个元素以反映 array2 中的值,或者是具有更新值的相同 array1。

使用一些更现代的语法来做到这一点的最佳方法是什么? 我打开了几个选项,所以我可以比较。 例如,我猜测 array1 中的匹配对象可以替换为 array2 对象,或者可以通过对元素进行某种迭代来更新 array1 对象中的单个元素。

var arra1 = [{
    "_id" : ObjectId("583f6e6d14c8042dd7c979e6"),
    "transid" : 1,
    "acct" : "acct1",
    "transdate" : ISODate("2012-01-31T05:00:00.000Z"),
    "category" : "category1",
    "amount" : 103 
}, {
    "_id" : ObjectId("583f6e6d14c8042dd7c2132t6"),
    "transid" : 2,
    "acct" : "acct2",
    "transdate" : ISODate("2012-01-31T05:00:00.000Z"),
    "category" : "category2",
    "amount" : 103 
}]

var arra2 = [{
    "_id" : ObjectId("583f6e6d14c8042dd7c979e6"),
    "transid" : 1,
    "acct" : "acct2",
    "transdate" : ISODate("2012-01-31T05:00:00.000Z"),
    "category" : "category5",
    "amount" : 107 
}, {
    "_id" : ObjectId("583f6e6d14c8042dd7c2132t6"),
    "transid" : 2,
    "acct" : "acct2",
    "transdate" : ISODate("2012-01-31T05:00:00.000Z"),
    "category" : "category2",
    "amount" : 103 
}, {
    "_id" : ObjectId("583f6e6d14c8042dd7c2132t6"),
    "transid" : 3,
    "acct" : "acct2",
    "transdate" : ISODate("2016-07-31T05:00:00.000Z"),
    "category" : "category3",
    "amount" : 103 
}]

我玩了一下,到目前为止有这样的东西。 它不起作用,但这就是我正在寻找的逻辑类型。

arr1.forEach(item => 
  if (arr2.indexOf(item.transid) != -1){
    item.category = arr2.indexOf(item.transid).category
  }
)
arra1 = arra1.map(item => {
  const item2 = arra2.find(i2 => i2.transid === item.transid);
  return item2 ? { ...item, ...item2 } : item;
});

arra1 arra1 在映射函数上尝试在arra2中找到相关项,如果找到,则将这两个项与 object spread 合并,如果没有,则返回原始项。 请注意,最后传播item2对合并至关重要,因此您使用item2中的值覆盖,但保留item1中未被覆盖的值。

为此使用 reduce

let res = arr2.reduce((a,b) => {
    let a1 = arr1.find(e => e.transid === b.transid) || {};
    return a.concat(Object.assign(a1, b));
},[]);

console.log(res);

 let arr1 = [{ "_id": ObjectId("583f6e6d14c8042dd7c979e6"), "transid": 1, "acct": "acct1", "transdate": ISODate("2012-01-31T05:00:00.000Z"), "category": "category1", "amount": 103 }, { "_id": ObjectId("583f6e6d14c8042dd7c2132t6"), "transid": 2, "acct": "acct2", "transdate": ISODate("2012-01-31T05:00:00.000Z"), "category": "category2", "amount": 103 }] let arr2 = [{ "_id": ObjectId("583f6e6d14c8042dd7c979e6"), "transid": 1, "acct": "acct2", "transdate": ISODate("2012-01-31T05:00:00.000Z"), "category": "category5", "amount": 107 }, { "_id": ObjectId("583f6e6d14c8042dd7c2132t6"), "transid": 2, "acct": "acct2", "transdate": ISODate("2012-01-31T05:00:00.000Z"), "category": "category2", "amount": 103 }, { "_id": ObjectId("583f6e6d14c8042dd7c2132t6"), "transid": 3, "acct": "acct2", "transdate": ISODate("2016-07-31T05:00:00.000Z"), "category": "category3", "amount": 103 }] // MOCK function ObjectId(i) { return i; } function ISODate(i) { return i; } // let res = arr2.reduce((a, b) => { let a1 = arr1.find(e => e.transid === b.transid) || {}; return a.concat(Object.assign(a1, b)); }, []); console.log(res);

您应该使用方法find来检索匹配的项目

arr1.forEach(item1 => {
      var itemFromArr2 = arr2.find(item2 => item2.transid == item1.transid);

      if (itemFromArr2) {
         item1.category = itemFromArr2.category;
      }
   }
)

暂无
暂无

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

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