[英]Method to merge / join two or more arrays of objects by key javascript
我正在尝试合并两个或更多 arrays 但它无法正常工作。
我有两个 arrays arrCustomer 和 arrCustomerDetails。 arrays 都将 CustomerID 作为键,但我希望 arrCustomerDetails 中的所有值都合并到数组 arrCustomer 的属性中。 我尝试使用 _merge 但它只给出了 arrCustomer 的计数。
例子:
const arrCustomer = [
{ id: 1, name: "a" },
{ id: 2, name: "b" },
{ id: 3, name: "c" }
];
const arrCustomerDetail = [
{ id: 1, location: "jupiter", group: "C" },
{ id: 1, location: "venus", group: "G" },
{ id: 2, location: "mars", group: "D" }
];
const expecteResult = [
{ id: 1, name: "a", location: "jupiter", group: "C" },
{ id: 1, name: "a", location: "venus", group: "G" },
{ id: 2, name: "b", location: "mars", group: "D" },
{ id: 3, name: "c" location: "", group: "" }
]
这有效但速度很慢:
let combinedData = [];
arrCustomerDetail.map((element) => {
combinedData.push({
...element,
...arrCustomer.find(
(customer) => customer.id=== element.id
),
});
});
arrCustomer.map((customer) => {
if (
combinedData.findIndex(
(detail) => detail.id=== customer.id
) === -1
)
combinedData.push({
...customer,
});
});
谢谢大家
(加入/合并/组合)多个 arrays 对象通过键组合。
更新,也可以在运行时为多个数组进行此连接。 你只需要定义一个主矩阵并在矩阵矩阵上创建一个过滤器,按照这个例子。
我希望这个例子能有所帮助,它工作得很好,对于你给出的例子,只需编辑下面的例子,你就会看到结果,我什至开了个玩笑,合并了所有东西,它工作正常。 请记住,它将始终将主矩阵视为基础。
"use strict"; const arrCustomer1 = [ { id: 1, name: "a" }, { id: 2, name: "b" } ]; const arrCustomerDetail1 = [ { id: 1, location: "jupiter", group: "C" }, { id: 2, location: "mars", group: "D" } ]; const wantedArray1 = [ { id: 1, name: "a", location: "jupiter", group: "C" }, { id: 2, name: "b", location: "mars", group: "D" } ]; const arrCustomer2 = [ { id: 1, name: "a" }, { id: 2, name: "b" }, { id: 3, name: "c" } ]; const arrCustomerDetail2 = [ { id: 1, location: "jupiter", group: "C" }, { id: 2, location: "mars", group: "D" } ]; const wantedArray2 = [ { id: 1, name: "a", location: "jupiter", group: "C" }, { id: 2, name: "b", location: "mars", group: "D" }, { id: 3, name: "c" } //or blanks for the other fields location group ]; const arrCustomer3 = [ { id: 1, name: "a" }, { id: 2, name: "b" }, { id: 3, name: "c" } ]; const arrCustomerDetail3 = [ { id: 1, location: "jupiter", group: "C" }, { id: 1, location: "venus", group: "G" }, { id: 2, location: "mars", group: "D" } ]; const wantedArray3 = [ { id: 1, name: "a", location: "jupiter", group: "C" }, { id: 1, name: "a", location: "venus", group: "G" }, { id: 2, name: "b", location: "mars", group: "D" }, { id: 3, name: "c" } //or blanks for the other fields location group ]; const combinedArrays = [arrCustomerDetail1, wantedArray1, arrCustomer2, arrCustomerDetail2, wantedArray2, arrCustomer3, arrCustomerDetail3, wantedArray3]; function joinArrByKey(primaryArr, combinedArrays, key) { return primaryArr.map((eArr1) => { const filter = new Array().concat.apply([], combinedArrays).filter((eArr2) => eArr2.id === eArr1[key]); return filter.length? Object.assign({}, eArr1, ...filter): eArr1; }); } console.log(joinArrByKey(arrCustomer1, combinedArrays, 'id')); console.log(joinArrByKey(arrCustomer2, combinedArrays, 'id')); console.log(joinArrByKey(arrCustomer3, combinedArrays, 'id'));
( Join / Merge / Combine ) 只针对两个 arrays by key id,速度更快。
您可以像这样创建自己的 function 并与所有应用程序共享,而不是使用 _lodash。
"use strict"; const arr1 = [ { id: 1, name: "a" }, { id: 2, name: "b" }, ]; const arr2 = [ { id: 1, price: 10 }, { id: 2, price: 20 }, ]; function joinArrById(primaryArr, secondArray) { return primaryArr.map((eArr1) => { const find = secondArray.find((eArr2) => eArr2.id === eArr1.id); return find? Object.assign(Object.assign({}, eArr1), find): eArr1; }); } console.log(joinArrById(arr1, arr2));
如果上述解决方案不适合您,请使用 _lodash 的解决方案。
import * as _ from "lodash";
const arr1 = [
{ id: 1, name: "a" },
{ id: 2, name: "b" },
];
const arr2 = [
{ id: 1, price: 10 },
{ id: 2, price: 20 },
];
var arrJoin = _.map(arr1, (obj: { id: number }) => {
return _.assign(
obj,
_.find(arr2, {
id: obj.id,
})
);
});
console.log(arrJoin);
Lodash 并不真正支持将列表合并在一起并关闭 object 属性来这样做。 我会做这样的事情,然后滚动我自己的merge()
function。
[注意:此merge()
function 以线性时间运行 — O(n)。]
const _ = require('lodash');
function merge(selectKey, keyComparer, ...lists) {
const map = new Map();
for (const list of lists) {
for (const item of list) {
const key = selectKey(item);
let value = map.get(key);
if (value === undefined) {
map.set(key, [item]);
}
else {
value.push(item);
}
}
}
const merged = Array.from(map.keys())
.sort(keyComparer)
.map( k => _.merge( {}, ...map.get(k) ) );
return merged;
}
其中 * selectKey
是一个 function,它传递一个 object 并返回它的键,* keyComparer
是一个 function,它接受两个键x
和y
,并返回一个数字指示,指示x
是小于、等于还是大于 `y '. 惯例是
x
小于y
x
和y
相等x
大于y
一旦你有了它,就很容易了:
const arr1 = [
{ id: 1, name: 'a' },
{ id: 2, name: 'b' },
{ id: 3, name: 'c' },
];
const arr2 = [
{ id: 1, location: 'jupiter', group: 'C' },
{ id: 4, location: 'venus', group: 'E' },
{ id: 2, location: 'mars', group: 'D' },
];
const idComparer = (x, y) => Math.sign(x - y);
const selectKey = o => o.id;
const merged = merge(selectKey, idComparer, arr1, arr2 );
这里的返回值为
[
{ id: 1 , name: "a" , location: "jupiter" , group: "C" } ,
{ id: 2 , name: "b" , location: "mars" , group: "D" } ,
{ id: 3 , name: "c" } ,
{ id: 4 , location: "venus" , group: "E" } ,
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.