gpt4 book ai didi

javascript - 如何合并对象数组只保留第一个数组中的属性

转载 作者:行者123 更新时间:2023-11-30 13:58:00 24 4
gpt4 key购买 nike

我有一个工作函数,它通过属性 a1 和 a1 合并两个对象数组。该解决方案基于对一些类似问题的回答,但我的要求是仅保留原始数组 a1 中的属性。

如何调整此函数以返回仅包含第一个数组中的属性的对象数组?

这是一个带有示例数据的 JS fiddle。请注意,输出记录到控制台。另请注意,解决方案必须使用 vanilla Javascript ES6。

https://jsfiddle.net/dba9r3sf/

const a1 = [
{
FirstName: "John",
LastName: "Doe",
Age: 33,
Username: "jdoe"
},
{
FirstName: "Mary",
LastName: "Bloom",
Age: 63,
Username: "mbloom"
},
{
FirstName: "Alex",
LastName: "Arias",
Age: 21,
Username: "aarias"
}
];

const a2 = [
{
FirstName: "Johnathan",
LastName: "Doe",
Age: 34,
Username: "jdoe",
Job: "Graphic Designer"
},
{
FirstName: "Mary-Anne",
LastName: "Bloom",
Age: 64,
Username: "mbloom",
Job: "Investor"
},
{
FirstName: "Alex",
LastName: "Arias",
Age: 22,
Username: "aarias",
Job: "Student"
}
];

/**
* Merge an array of objects by property
* @param {array} a1 array 1 destination to be merged into
* @param {array} a2 array 2 source to be merged, overwrites existing values in a1
* @param {string} prop name of property to match for merge
* TODO: Set properties that exist on a1 only otherwise ignore
*/
function mergeByProperty(a1, a2, prop) {

let merged = [];

for (let i = 0; i < a1.length; i++) {
merged.push({
...a1[i],
...(a2.find((itmInner) => itmInner[prop] === a1[i][prop]))
});
}

return merged;
}

let result = mergeByProperty(a1, a2, 'Username');
console.log(JSON.stringify(result));

// Output:
[{"FirstName":"Johnathan","LastName":"Doe","Age":34,"Username":"jdoe","Job":"Graphic Designer"},{"FirstName":"Mary-Anne","LastName":"Bloom","Age":64,"Username":"mbloom","Job":"Investor"},{"FirstName":"Alex","LastName":"Arias","Age":22,"Username":"aarias","Job":"Student"}]

// Desired output (no "Job" property because that does not exist in the first array of objects):
[{"FirstName":"Johnathan","LastName":"Doe","Age":34,"Username":"jdoe"},{"FirstName":"Mary-Anne","LastName":"Bloom","Age":64,"Username":"mbloom},{"FirstName":"Alex","LastName":"Arias","Age":22,"Username":"aarias"}]

最佳答案

您可以从第一个数组中获取键,然后从第二个数组中选择相应的键/值

const a1 = [{FirstName: "John",LastName: "Doe",Age: 33,Username: "jdoe"},{FirstName: "Mary",LastName: "Bloom",Age: 63,Username: "mbloom"},{FirstName: "Alex",LastName: "Arias",Age: 21,Username: "aarias"}];
const a2 = [{FirstName: "Johnathan",LastName: "Doe",Age: 34,Username: "jdoe",Job: "Graphic Designer"},{FirstName: "Mary-Anne",LastName: "Bloom",Age: 64,Username: "mbloom",Job: "Investor"},{FirstName: "Alex",LastName: "Arias",Age: 22,Username: "aarias",Job: "Student"}];

function mergeByProperty(a1, a2, prop) {
let merged = [];
for (let i = 0; i < a1.length; i++) {
let found = a2.find((itmInner) => itmInner[prop] === a1[i][prop])
if(found){
found = Object.keys(a1[0]).reduce((op,inp)=>{
op[inp] = found[inp]
return op
},{})
}
merged.push({
...a1[i],
...found
});
}
return merged;
}

let result = mergeByProperty(a1, a2, 'Username');
console.log((result));

关于javascript - 如何合并对象数组只保留第一个数组中的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56840041/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com