admin管理员组文章数量:1327966
I'm trying to create a new array of object with the merged value if some values end up to be equal.
For exemple:
const arr1 = [{name: "Sven", firstName: "Alan", age: 24, profile: "Hello, i'm Alan"}, {name: "Mign", firstName: "Alex", age: 44, profile: "Hello, i'm Alex"}];
const arr2 = [{location: "Miami", alias: "ALAN", isAlive: true}, {location: "Los Angeles", alias: "Ethic", isAlive: true}]
In that case the firstName of the first array is matching with the alias of the second array, in that case i would like to merge those to object together and return them like so:
const newArray = [{name: "Sven", firstName: "Alan", age: 24, profile: "Hello, i'm Alan", isAlive: true, location: "Miami"}]
I have tried multiple solution but the closest i could get to make it work is:
const newArray = arr1.map((item, i) => {
const mapping = arr2.map(sym => sym.firstName.toLowerCase());
if (mapping.includes(item.alias.toLowerCase())) {
return Object.assign({}, item, arr2[i]);
}
});
In that case i'm merging with the index in the object.assign so my solution is not correct, everything will be mixed.
I'm trying to create a new array of object with the merged value if some values end up to be equal.
For exemple:
const arr1 = [{name: "Sven", firstName: "Alan", age: 24, profile: "Hello, i'm Alan"}, {name: "Mign", firstName: "Alex", age: 44, profile: "Hello, i'm Alex"}];
const arr2 = [{location: "Miami", alias: "ALAN", isAlive: true}, {location: "Los Angeles", alias: "Ethic", isAlive: true}]
In that case the firstName of the first array is matching with the alias of the second array, in that case i would like to merge those to object together and return them like so:
const newArray = [{name: "Sven", firstName: "Alan", age: 24, profile: "Hello, i'm Alan", isAlive: true, location: "Miami"}]
I have tried multiple solution but the closest i could get to make it work is:
const newArray = arr1.map((item, i) => {
const mapping = arr2.map(sym => sym.firstName.toLowerCase());
if (mapping.includes(item.alias.toLowerCase())) {
return Object.assign({}, item, arr2[i]);
}
});
In that case i'm merging with the index in the object.assign so my solution is not correct, everything will be mixed.
Share edited Jul 22, 2021 at 2:46 AbsoluteBeginner 2,2533 gold badges14 silver badges24 bronze badges asked Jul 21, 2021 at 5:17 Eliane BheaverEliane Bheaver 811 silver badge2 bronze badges4 Answers
Reset to default 4Use array spread to achive merging
const arr1 = [{name: "Sven", firstName: "Alan", age: 24, profile: "Hello, i'm Alan"}, {name: "Mign", firstName: "Alex", age: 44, profile: "Hello, i'm Alex"}];
const arr2 = [{location: "Miami", alias: "ALAN", isAlive: true}, {location: "Los Angeles", alias: "Ethic", isAlive: true}]
const mergedData = arr1.map(data=>({
...data,
...arr2.find(newData=>newData.alias.toLowerCase() == data.firstName.toLowerCase())
}))
console.log(mergedData)
Try this:
const arr1 = [{name: "Sven", firstName: "Alan", age: 24, profile: "Hello, i'm Alan"}, {name: "Mign", firstName: "Alex", age: 44, profile: "Hello, i'm Alex"}];
const arr2 = [{location: "Miami", alias: "ALAN", isAlive: true}, {location: "Los Angeles", alias: "Ethic", isAlive: true}]
output = [];
arr1.forEach(item => {
const match = arr2.find(
item2 => item.firstName.toLowerCase() === item2.alias.toLowerCase()
);
if (match) {
output.push({ ...item, ...match });
}
});
console.log(output);
Try using Array.reduce
in javascript.
const arr1 = [
{ name: "Sven", firstName: "Alan", age: 24, profile: "Hello, i'm Alan" },
{ name: "Mign", firstName: "Alex", age: 44, profile: "Hello, i'm Alex" }
];
const arr2 = [
{ location: "Miami", alias: "ALAN", isAlive: true },
{ location: "Los Angeles", alias: "Ethic", isAlive: true }
];
const newArray = arr1.reduce((acc, curr) => {
const searchNode = arr2.find((node) => node.alias.toLowerCase() === curr.firstName.toLowerCase());
const newNode = searchNode ? { ...searchNode, ...curr } : null;
if (newNode) {
acc.push(newNode)
}
return acc;
}, []);
console.log(newArray);
Here is a fairly inefficient but very clear solution.
const arr1 = [{name: "Sven", firstName: "Alan", age: 24, profile: "Hello, i'm Alan"}, {name: "Mign", firstName: "Alex", age: 44, profile: "Hello, i'm Alex"}];
const arr2 = [{location: "Miami", alias: "ALAN", isAlive: true}, {location: "Los Angeles", alias: "Ethic", isAlive: true}];
arr3 = [];
for (const d1 of arr1) {
for (const d2 of arr2) {
if (d1.firstName.toLowerCase() == d2.alias.toLowerCase()) {
arr3.push(Object.assign({}, d1, d2));
}
}
}
console.log(arr3);
本文标签: javascriptMerge two array of object if a value is matchingStack Overflow
版权声明:本文标题:javascript - Merge two array of object if a value is matching - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742220783a2435416.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论