且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

根据ID将数组中的n个对象合并到一个数组中

更新时间:2022-12-11 08:14:49

在您的代码中,如果您已经有r[id],则没有分配其余值.因此,请像这样更改它:

In your code, if you already have a r[id], you didn't assign the rest values. So change it like this:

const result = Object.values(
  array.reduce((r, { data }) => {
    Object.entries(data).forEach(([id, { ...el }]) => {
      r[id] = {
        ...r[id], // this is the point
        id,
        ...el
      };
    });
    return r;
  }, {})
);