admin管理员组文章数量:1414614
I want to merge two arrays of objects. The keys are the same but the values might not always be the same.
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
I want to merge two arrays of objects. The keys are the same but the values might not always be the same.
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
Share
Improve this question
edited Mar 4, 2018 at 16:35
str
45.1k18 gold badges114 silver badges134 bronze badges
asked Dec 8, 2017 at 11:38
SayranSayran
1631 gold badge2 silver badges11 bronze badges
2
-
What is there both has the same key say
goal
? – Nishant Commented Dec 8, 2017 at 11:45 - Possible duplicate of Merge 2 arrays of objects – Nishant Commented Dec 8, 2017 at 11:48
3 Answers
Reset to default 4You 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; }
With dynamic 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; }
Here's a Python solution. This modifies g
, which you may or may not want.
c_by_id = {d['id']: d for d in c}
for item in g:
item.update(c_by_id.get(item['id']), {})
This can be easily achieved by underscore functions _.uniq
and _.union
.
Just use this:
var finalData = _.uniq(_.union(c, g), function (ele) {
return ele.id
})
This will return you what you are looking for.
本文标签:
版权声明:本文标题:javascript - Merge two array of objects with same keys, some object won't have the same value? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745173514a2646117.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论