【问题标题】:How can I update an array based on another array on matching index?如何根据匹配索引上的另一个数组更新数组?
【发布时间】:2017-10-05 17:32:47
【问题描述】:

我有以下两个对象数组。我想更新 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
  }
)

【问题讨论】:

  • 像 ES6 一样现代?意思是不担心在所有浏览器上工作?
  • 是的 ES6。我使用 babel,所以用于本地开发和学习目的,这很好。但我对您可以共享的任何非 ES6 解决方案持开放态度,以实现更大的兼容性。
  • 是否有可能在任一阵列中出现重复的 transid?意思是每个数组 0 或 1 个 transid。如果数组 2 可能有 3 个 transid 1,这将消除一些选项。
  • 两个数组中都没有重复项。我编辑了我的帖子以添加一些我正在尝试设置的逻辑的 sn-p。

标签: javascript arrays


【解决方案1】:
arra1 = arra1.map(item => {
  let item2 = arra2.find(i2 => i2.transid === item.transid);
  return item2 ? { ...item, ...item2 } : item;
});

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

【讨论】:

    【解决方案2】:

    为此使用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);

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-05-17
        • 1970-01-01
        • 1970-01-01
        • 2016-02-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多