猿问

如何按键合并两个对象数组并在单个对象数组中保留唯一键?

我想合并两个对象数组,其中具有相同 ID 的对象将合并属性,而具有唯一 ID 的对象将成为合并数组中其自己的对象。以下代码执行第一部分,其中相似的 ID 将合并,但如何将arr2中具有唯一 id 的对象保留在合并数组中,并使其适用于不同长度的数组?


预期输出:


[

  {

    "id": "1",

    "date": "2017-01-24",

    "name": "test"

  },

  {

    "id": "2",

    "date": "2017-01-22",

    "bar": "foo"

  }

  { "id": "3",

    "foo": "bar",

  }

]

代码:


let arr1 = [{

    id: '1',

    createdDate: '2017-01-24'

  },

  {

    id: '2',

    createdDate: '2017-01-22'

  },

];


let arr2 = [{

    id: '1',

    name: 'test'

  },

  {

    id: '3',

    foo: 'bar'

  },

  {

    id: '2',

    bar: 'foo'

  },

];


let merged = [];


for (let i = 0; i < arr1.length; i++) {

  merged.push({

      ...arr1[i],

      ...arr2.find((itmInner) => itmInner.id === arr1[i].id),

    },


  );

}


console.log(merged);


慕斯709654
浏览 181回答 4
4回答

MM们

迭代较大的数组,即包含较小数组的数组:let arr1=[{id:"1",createdDate:"2017-01-24"},{id:"2",createdDate:"2017-01-22"}],arr2=[{id:"1",name:"test"},{id:"3",foo:"bar"},{id:"2",bar:"foo"}];const merged = arr2.map(item => ({&nbsp; ...arr1.find(({ id }) => id === item.id),&nbsp; ...item}));console.log(merged);(如果顺序很重要,您也可以在之后排序)如果您事先不知道哪个/如果一个将包含另一个,那么首先使用一个对象通过 ID 索引合并的对象:let arr1=[{id:"1",createdDate:"2017-01-24"},{id:"2",createdDate:"2017-01-22"}],arr2=[{id:"1",name:"test"},{id:"3",foo:"bar"},{id:"2",bar:"foo"}];const resultObj = Object.fromEntries(&nbsp; arr1.map(&nbsp; &nbsp; item => [item.id, { ...item }]&nbsp; ));for (const item of arr2) {&nbsp; if (!resultObj[item.id]) {&nbsp; &nbsp; resultObj[item.id] = item;&nbsp; } else {&nbsp; &nbsp; Object.assign(resultObj[item.id], item);&nbsp; }}const merged = Object.values(resultObj);console.log(merged);

MMMHUHU

您可以编写一个函数将数组缩减为一个对象,然后从该对象中提取值,该对象将返回您想要的值。你可以看到下面的代码:let arr1 = [&nbsp; {&nbsp; &nbsp; id: '1',&nbsp; &nbsp; createdDate: '2017-01-24',&nbsp; },&nbsp; {&nbsp; &nbsp; id: '2',&nbsp; &nbsp; createdDate: '2017-01-22',&nbsp; },];let arr2 = [&nbsp; {&nbsp; &nbsp; id: '1',&nbsp; &nbsp; name: 'test',&nbsp; },&nbsp; {&nbsp; &nbsp; id: '3',&nbsp; &nbsp; foo: 'bar',&nbsp; },&nbsp; {&nbsp; &nbsp; id: '2',&nbsp; &nbsp; bar: 'foo',&nbsp; },];function merge(arr1 = [], arr2 = []) {&nbsp; return Object.values(&nbsp; &nbsp; arr1.concat(arr2).reduce(&nbsp; &nbsp; &nbsp; (acc, curr) => ({&nbsp; &nbsp; &nbsp; &nbsp; ...acc,&nbsp; &nbsp; &nbsp; &nbsp; [curr.id]: { ...(acc[curr.id] ?? {}), ...curr },&nbsp; &nbsp; &nbsp; }),&nbsp; &nbsp; &nbsp; {}&nbsp; &nbsp; )&nbsp; );}const merged = merge(arr1, arr2);输出:[&nbsp; {&nbsp; &nbsp; "id": "1",&nbsp; &nbsp; "createdDate": "2017-01-24",&nbsp; &nbsp; "name": "test"&nbsp; },&nbsp; {&nbsp; &nbsp; "id": "2",&nbsp; &nbsp; "createdDate": "2017-01-22",&nbsp; &nbsp; "bar": "foo"&nbsp; },&nbsp; {&nbsp; &nbsp; "id": "3",&nbsp; &nbsp; "foo": "bar"&nbsp; }]

守候你守候我

arr1您可以创建包含和arr2group by键的元素的新对象,id如下所示,合并的数组将存储在对象值上。您可以使用Object.valuesfunc 获取对象值。let arr1 = [{&nbsp; &nbsp; id: '1',&nbsp; &nbsp; createdDate: '2017-01-24'&nbsp; },&nbsp; {&nbsp; &nbsp; id: '2',&nbsp; &nbsp; createdDate: '2017-01-22'&nbsp; },];let arr2 = [{&nbsp; &nbsp; id: '1',&nbsp; &nbsp; name: 'test'&nbsp; },&nbsp; {&nbsp; &nbsp; id: '3',&nbsp; &nbsp; foo: 'bar'&nbsp; },&nbsp; {&nbsp; &nbsp; id: '2',&nbsp; &nbsp; bar: 'foo'&nbsp; },];const groupById = {};for (let i = 0; i < Math.min(arr1.length, arr2.length); i ++) {&nbsp; if (arr1[i]) {&nbsp; &nbsp; groupById[arr1[i].id] = { ...groupById[arr1[i].id], ...arr1[i] };&nbsp; }&nbsp; if (arr2[i]) {&nbsp; &nbsp; groupById[arr2[i].id] = { ...groupById[arr2[i].id], ...arr2[i] };&nbsp; }}const merged = Object.values(groupById);console.log(merged);

茅侃侃

您可以采用单循环方法,将对象存储在哈希表中,并按 排序id。const&nbsp; &nbsp; mergeTo = (target, objects = {}) => o => {&nbsp; &nbsp; &nbsp; &nbsp; if (!objects[o.id]) target.push(objects[o.id] = {});&nbsp; &nbsp; &nbsp; &nbsp; Object.assign(objects[o.id], o);&nbsp; &nbsp; },&nbsp; &nbsp; array1 = [{ id: '1', createdDate: '2017-01-24' }, { id: '2', createdDate: '2017-01-22' }],&nbsp; &nbsp; array2 = [{ id: '1', name: 'test' }, { id: '3', foo: 'bar' }, { id: '2', bar: 'foo' }],&nbsp; &nbsp; merged = [],&nbsp; &nbsp; merge = mergeTo(merged);&nbsp; &nbsp;&nbsp;array1.forEach(merge);array2.forEach(merge);console.log(merged);.as-console-wrapper { max-height: 100% !important; top: 0; }
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答