合并两个具有相同键的对象数组,某些对象将不具有相同的值? [英] Merge two array of objects with same keys, some object won't have the same value?
问题描述
我想合并两个对象数组。键是相同的,但值可能并不总是相同。
I want to merge two arrays of objects. The keys are the same but the values might not always be the same.
任何解决方案最好在javascript中受到赞赏,但python解决方案也很好。
Any solution is appreciated preferably in javascript, but a python solution is fine as well.
以下是样本数据:
Here is the sample data:
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'} ];
我的预期输出应为:
My expected output should be :
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' }
}]
这是我当前的代码,它在某种程度上工作,但它不会添加它可能错过的键。
Here is my current code, it works to some degree but it doesn't add keys it might have missed.
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
推荐答案
你可以使用 地图
用于在同一对象中保留相同的 id
并且 Object.assign
用于创建独立对象。
You could use a Map
for keeping same id
in the same object and Object.assign
for creating independent objects.
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; }
没有减少和没有连续的版本。
Version without reduce and without 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; }
这篇关于合并两个具有相同键的对象数组,某些对象将不具有相同的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!