我尝试合并数组
var x =[{n:'s1', a:'sang', m1: 10}, {n:'s2', a:'sang', m1: 20}]
var y =[{n:'s1', a:'sang', m2: 20}, {n:'s2', a:'sang', m2: 20}]
after merging var a and var b I need result in var c like,
c = [{n:'s1', a:'sang', m1: 10, m2: 20},{{n:'s2', a:'sang', m1: 20,m2: 20}]
答案 0 :(得分:1)
您可以使用Object(assign)方法:
var x =[{n:'s1', a:'sang', m1: 10}, {n:'s2', a:'sang', m1: 20}]
var y =[{n:'s1', a:'sang', m2: 20}, {n:'s2', a:'sang', m2: 20}]
var c = [];
x.forEach(a => {
c.push( Object.assign({}, a, y.find( b => b.n === a.n) ) );
});
console.log( c );