根据匹配的ID合并两个对象列表

时间:2019-03-08 13:56:02

标签: javascript

如何在列表中的ID之间匹配的对象中添加一个key:value对 我想将uuid添加到list2,其中list 1和2之间的id相同。

我认为我需要结合使用过滤器和地图,但无法解决。有什么建议么?

列表1

OleDbDataAdapter da = new OleDbDataAdapter(command);

列表2

[ { uuid: 26, id: 317 }
  { uuid: 21, id: 451 },
  { uuid: 43, id: 504 }, ]

所需结果

[ { id: 317, coords: [ 33.4325301346224, 12.3742694854736 ] },
  { id: 451, coords: [ 43.4257858672581, 14.4208612450748 ] },
  { id: 504, coords: [ 33.4225091429188, 12.395770072937 ] } ]

1 个答案:

答案 0 :(得分:1)

在这里,如果有任何疑问,请随时提出。

list2.reduce((list, entry) => {
    const commonElement = list1.find(_entry => _entry.id === entry.id);

    if (commonElement) {
        list.push({
            ...entry,
            ...commonElement,
        });
    } else {
        list.push(entry);
    }

    return list;
}, []);
相关问题