合并Nodejs中的2个数组而不覆盖[重复]

ax6ht2ek  于 2023-02-18  发布在  Node.js
关注(0)|答案(1)|浏览(177)
    • 此问题在此处已有答案**:

Merge two arrays containing objects and remove duplicate values(7个答案)
9小时前关门了。
我想把两个数据库中的数组合并成一个而不覆盖数据,我尝试了不同的选项,如lodash合并,连接等,但没有运气。你能帮帮忙吗?
我的对象:

places: [
    { placeId: 2, place: 'a' },
    { placeId: 3, place: 'b' },
    { placeId: 4, place: 'c' },
    { placeId: 5, place: 'd' },
    { placeId: 6, place: 'e' }
  ]

places: [
    { placeId: 12, place: 'f' },
    { placeId: 13, place: 'g' },
    { placeId: 14, place: 'h' },
    { placeId: 21, place: 'i' },
    { placeId: 22, place: 'j' },
    { placeId: 29, place: 'k' },
    { placeId: 30, place: 'l' }
  ]

我想要这个

places:[
    { placeId: 2, place: 'a' },
    { placeId: 3, place: 'b' },
    { placeId: 4, place: 'c' },
    { placeId: 5, place: 'd' },
    { placeId: 6, place: 'e' },
    { placeId: 12, place: 'f' },
    { placeId: 13, place: 'g' },
    { placeId: 14, place: 'h' },
    { placeId: 21, place: 'i' },
    { placeId: 22, place: 'j' },
    { placeId: 29, place: 'k' },
    { placeId: 30, place: 'l' }

]
sgtfey8w

sgtfey8w1#

你可以这样做:
常量所有位置=位置1.concat(位置2)

常量所有位置= [...位置1,...位置2]

相关问题