合并Javascript中的两个数组对象

时间:2018-08-29 08:44:49

标签: javascript arrays arraylist merge

我一直试图将这两个数组对象合并为一个。 我尝试了在Stackoverflow上发布的多种解决方案,但是不知何故,我的问题与标准合并没有什么不同。

第一个对象(arrObj1)中每个数组的元素都需要根据键(id)与第二个对象(arrObj2)合并。

由于第二个对象多次具有相同的键值(id:1-两次,而id:2-2倍),因此我很难解决这个问题。

arrObj1 = [ 
    { id: 1,
    name: 'Jack',
    date: '09/05/2018',
    web_ref_id: '311102010201210'},
    { id: 2,
    name: 'Smith',
    date: '09/12/2018',
    web_ref_id: '1111201311111203'} ]
arrObj2 = [ 
   { id: 1,
    changes_id: 1,
    city: 'Chicago',
    Zip: '12345',
    street_name: 'Rockhill Dr',
    web_ref_id: '311102010201210'},
  { id: 1,
    changes_id: 1,
    city: 'Chicago',
    Zip: '12345',
    street_name: 'Cambridge Dr',
    web_ref_id: '311102010201210'},
  { id: 2,
    changes_id: 2,
    city: 'New York',
    Zip: '43435',
    street_name: 'Smithson Dr',
    web_ref_id: '1111201311111203' },
  { id: 2,
    db_changes_id: 2,
    changes_id: 2,
    city: 'New York',
    Zip: '43435',
    street_name: 'Lombard Blvd',
    web_ref_id: '1111201311111203' } ]

预期结果:

arrObj3 = [ 
   { id: 1,
    name: 'Jack',
    date: '09/05/2018',
    changes_id: 1,
    city: 'Chicago',
    Zip: '12345',
    street_name: 'Rockhill Dr',
    web_ref_id: '311102010201210'},
  { id: 1,
    name: 'Jack',
    date: '09/05/2018',
    changes_id: 1,
    city: 'Chicago',
    Zip: '12345',
    street_name: 'Cambridge Dr',
    web_ref_id: '311102010201210'},
  { id: 2,
    name: 'Smith',
    date: '09/12/2018'
    changes_id: 2,
    city: 'New York',
    Zip: '43435',
    street_name: 'Smithson Dr',
    web_ref_id: '1111201311111203' },
  { id: 2,
    name: 'Smith',
    date: '09/12/2018'
    db_changes_id: 2,
    changes_id: 2,
    city: 'New York',
    Zip: '43435',
    street_name: 'Lombard Blvd',
    web_ref_id: '1111201311111203' } ]

尝试过的解决方案:

var objArr3 = objArr1,
power = objArr2,
hash = new Map,
merged = power.map(a => {
    var o = {};
    Object.assign(o, a);
    hash.set(a.id, o);
    return o;
});
objArr3.forEach(a => hash.has(a.id) && Object.assign(hash.get(a.id), a));

不知何故,这跳过了插入第二个对象(objArr2)的第一个数组的操作。

1 个答案:

答案 0 :(得分:1)

您可以这样做:

let arrObj1 = [ { id: 1, name: 'Jack', date: '09/05/2018', web_ref_id: '311102010201210'}, { id: 2, name: 'Smith', date: '09/12/2018', web_ref_id: '1111201311111203'} ] 

let arrObj2 = [ { id: 1, changes_id: 1, city: 'Chicago', Zip: '12345', street_name: 'Rockhill Dr', web_ref_id: '311102010201210'}, { id: 1, changes_id: 1, city: 'Chicago', Zip: '12345', street_name: 'Cambridge Dr', web_ref_id: '311102010201210'}, { id: 2, changes_id: 2, city: 'New York', Zip: '43435', street_name: 'Smithson Dr', web_ref_id: '1111201311111203' }, { id: 2, db_changes_id: 2, changes_id: 2, city: 'New York', Zip: '43435', street_name: 'Lombard Blvd', web_ref_id: '1111201311111203' } ]

//Function

let result = arrObj2.map(e => {
 return {...arrObj1.find(l => l.id = e.id), ...e}
})
 
console.log(result)

文档: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax