如何在匹配索引的另一个数组的基础上更新数组? [英] How can I update an array based on another array on matching index?

查看:75
本文介绍了如何在匹配索引的另一个数组的基础上更新数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下两个对象数组.我想更新array1元素以反映与transid匹配的array2元素.因此,我的结果应该是一个新数组,其中array1中的两个元素已更新以反映array2中的值,或者是具有更新后值的同一array1.

I have the following two object arrays. I would like to update array1 elements to reflect array2 elements where the transid matches. So my result should be either a new array that has the two elements from array1 updated to reflect the values in array2, or the same array1 with the updated values.

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

What is the best way to do that with some of the more modern syntax? I open to several options so I can compare. For example, I'm guessing matching objects in array1 can just be replaced with array2 objects or the individual elements within the array1 objects can be updated through some sort of iteration over the elements.

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

我玩了一点,到目前为止有类似的东西.它不起作用,但这就是我要寻找的逻辑类型.

I played with it a bit and have something like this thus far. It's not working but that's the type of logic I'm looking for.

arr1.forEach(item => 
  if (arr2.indexOf(item.transid) != -1){
    item.category = arr2.indexOf(item.transid).category
  }
)

推荐答案

arra1 = arra1.map(item => {
  let item2 = arra2.find(i2 => i2.transid === item.transid);
  return item2 ? { ...item, ...item2 } : item;
});

arra1中的所有元素映射到arra1本身.在映射功能上,尝试在arra2中找到相关项,并在找到对象后将其与对象散布合并,如果找不到,则返回原始项.请注意,最后传播item2对于合并至关重要,因此您可以使用item2中的值覆盖,但将未覆盖的内容保留在item1中.

Map all elements in arra1 into arra1 itself. On the mapping function try to find the correlative item in arra2 and merge the two items with object spread if its found, if not, return the original item. Notice that spreading item2 last is crucial to the merge, so you overwrite with the values from item2 but keep those in item1 that were not overwritten.

这篇关于如何在匹配索引的另一个数组的基础上更新数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆