比较数组中对象中的键与另一个数组中对象中的另一个键,如果值相等则将它们合并在一起
Compare a key in an Object in an Array with another key in an Object in Another array and merge them together if the values are equal
我有两个数组:
var array1 = [
{ id: 1, name: 'Name 1' },
{ id: 2, name: 'Name 2' },
...
];
var array2 = [
{ someId: '1', someField: 'Some Value 1' },
{ someId: '2', someField: 'Some Value 2' },
...
];
array1
将有来自后端的对象,每批 30 个。一旦我得到一批,我就从这个数组中提取 Ids
,然后调用另一个 API获取这些 ID 的 array2
。
最终,我想要一个这样的数组:
var array3 = [
{ id: 1, name: 'Name 1', someOtherField: 'Some Value 1' },
{ id: 2, name: 'Name 2', someOtherField: 'Some Value 2' },
...
];
我可以这样做:
ids = array1.map(item => item.id);
var resultingArray = array2.map((item, index) => {
return array1[index].someOtherField = item.someField
});
但是由于我是批量拥有array1的项目,所以很难正确维护索引。
我该怎么做?
为 array2
个 ID 建立查找 table:
const ids = new Map(array2.map(el => [el.id, /*to*/ el]));
然后从 array1
添加数据就像:
for(const el of array2) {
if(ids.has(el.id)) {
// Merge properties
Object.assign(ids.get(el.id), el);
} else {
// Add to Map and array
array1.push(el);
ids.set(el.id, el);
}
}
你可以组合两个数组,并在字典上使用Array.reduce()
with some destructuring to get both id
and someId
as id
, and store everything on a dictionary (POJO). We get an array by using Object.values()
:
const array1 = [{ id: 1, name: 'Name 1' }, { id: 2, name: 'Name 2' }];
const array2 = [{ someId: '1', someField: 'Some Value 1' }, { someId: '2', someField: 'Some Value 2' }];
const result = Object.values(
// combine both arrays
[...array1, ...array2]
// use destructuring to get someId/id as id, and the rest of objects' props
.reduce((r, { someId, id = someId, ...rest }) => ({
...r, // spread the previous accumulator
[id]: { id, ...r[id], ...rest } // add the current object, with the id, props, and previous data if any
}), {})
);
console.log(result);
我有两个数组:
var array1 = [
{ id: 1, name: 'Name 1' },
{ id: 2, name: 'Name 2' },
...
];
var array2 = [
{ someId: '1', someField: 'Some Value 1' },
{ someId: '2', someField: 'Some Value 2' },
...
];
array1
将有来自后端的对象,每批 30 个。一旦我得到一批,我就从这个数组中提取 Ids
,然后调用另一个 API获取这些 ID 的 array2
。
最终,我想要一个这样的数组:
var array3 = [
{ id: 1, name: 'Name 1', someOtherField: 'Some Value 1' },
{ id: 2, name: 'Name 2', someOtherField: 'Some Value 2' },
...
];
我可以这样做:
ids = array1.map(item => item.id);
var resultingArray = array2.map((item, index) => {
return array1[index].someOtherField = item.someField
});
但是由于我是批量拥有array1的项目,所以很难正确维护索引。
我该怎么做?
为 array2
个 ID 建立查找 table:
const ids = new Map(array2.map(el => [el.id, /*to*/ el]));
然后从 array1
添加数据就像:
for(const el of array2) {
if(ids.has(el.id)) {
// Merge properties
Object.assign(ids.get(el.id), el);
} else {
// Add to Map and array
array1.push(el);
ids.set(el.id, el);
}
}
你可以组合两个数组,并在字典上使用Array.reduce()
with some destructuring to get both id
and someId
as id
, and store everything on a dictionary (POJO). We get an array by using Object.values()
:
const array1 = [{ id: 1, name: 'Name 1' }, { id: 2, name: 'Name 2' }];
const array2 = [{ someId: '1', someField: 'Some Value 1' }, { someId: '2', someField: 'Some Value 2' }];
const result = Object.values(
// combine both arrays
[...array1, ...array2]
// use destructuring to get someId/id as id, and the rest of objects' props
.reduce((r, { someId, id = someId, ...rest }) => ({
...r, // spread the previous accumulator
[id]: { id, ...r[id], ...rest } // add the current object, with the id, props, and previous data if any
}), {})
);
console.log(result);