gpt4 book ai didi

javascript - 合并两个具有相同键的对象数组,某些对象不会具有相同的值?

转载 作者:太空宇宙 更新时间:2023-11-03 15:49:33 25 4
gpt4 key购买 nike

我想合并两个对象数组。键相同,但值可能并不总是相同。

最好在 javascript 中欣赏任何解决方案,但 python 解决方案也很好。

这是示例数据:

var g= [ 
{ id: 36, name: 'AAA', goal: 'yes' , 'random':27},
{ id: 40, name: 'BBB', goal: 'yes' },
{ id: 39, name: 'JJJ', goal: 'yes' },
{ id: 27, name: 'CCC', goal: 'yes' , lag: "23.3343"}];


var c= [
{ id: 36, name: 'AAA', goal: 'yes', color:"purple" },
{ id: 40, name: 'BBB', circle: 'yes', color:"purple" },
{ id: 100, name: 'JJJ', circle: 'yes'} ];

我的预期输出应该是:

 var finalData = [{
{ id: 36, name: 'AAA', goal: 'yes' ,'random':27, color:"purple"},
{ id: 40, name: 'BBB', circle: 'yes', color:"purple"},
{ id: 39, name: 'JJJ', goal: 'yes' },
{ id: 27, name: 'CCC', goal: 'yes' ,lag: "23.3343"},
{ id: 100, name: 'JJJ', circle: 'yes' }

}]

这是我当前的代码,它在某种程度上可以工作,但不会添加它可能遗漏的键。

var finalData = [];
for(var i in g){
var shared = false;
for (var j in c)
if (c[j].name == g[i].name) {
shared = true;
break;
}
if(!shared) finalData.push(g[i])
}
finalData = finalData.concat(c);

finalData

最佳答案

你可以使用 Map在同一对象中保持相同的 idObject.assign用于创建独立对象。

var g = [{ id: 36, name: 'AAA', goal: 'yes', 'random': 27 }, { id: 40, name: 'BBB', goal: 'yes' }, { id: 39, name: 'JJJ', goal: 'yes' }, { id: 27, name: 'CCC', goal: 'yes', lag: "23.3343" }],
c = [{ id: 36, name: 'AAA', goal: 'yes', color: "purple" }, { id: 40, name: 'BBB', circle: 'yes', color: "purple" }, { id: 100, name: 'JJJ', circle: 'yes' }],
map = new Map,
result = g.concat(c).reduce(function (r, o) {
var temp;
if (map.has(o.id)) {
Object.assign(map.get(o.id), o);
} else {
temp = Object.assign({}, o);
map.set(temp.id, temp);
r.push(temp);
}
return r;
}, []);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

没有 reduce 和 concat 的版本。

function merge(o) {
var temp;
if (map.has(o.id)) {
Object.assign(map.get(o.id), o);
return;
}
temp = Object.assign({}, o);
map.set(temp.id, temp);
result.push(temp);
}

var g = [{ id: 36, name: 'AAA', goal: 'yes', 'random': 27 }, { id: 40, name: 'BBB', goal: 'yes' }, { id: 39, name: 'JJJ', goal: 'yes' }, { id: 27, name: 'CCC', goal: 'yes', lag: "23.3343" }],
c = [{ id: 36, name: 'AAA', goal: 'yes', color: "purple" }, { id: 40, name: 'BBB', circle: 'yes', color: "purple" }, { id: 100, name: 'JJJ', circle: 'yes' }],
map = new Map,
result = [];

[g, c].forEach(function (a) {
a.forEach(merge);
});

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

使用动态 key 。

function mergeBy(key, data) {
return Array.from(data
.reduce((m, o) => m.set(o[key], { ...m.get(o[key]), ...o }), new Map)
.values()
);
}

var g = [{ id: 36, name: 'AAA', goal: 'yes', 'random': 27 }, { id: 40, name: 'BBB', goal: 'yes' }, { id: 39, name: 'JJJ', goal: 'yes' }, { id: 27, name: 'CCC', goal: 'yes', lag: "23.3343" }],
c = [{ id: 36, name: 'AAA', goal: 'yes', color: "purple" }, { id: 40, name: 'BBB', circle: 'yes', color: "purple" }, { id: 100, name: 'JJJ', circle: 'yes' }],
result = mergeBy('id', [...g, ...c]);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

关于javascript - 合并两个具有相同键的对象数组,某些对象不会具有相同的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47713659/

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