Combine json arrays by key, javascript
Two one-liners:
with lodash:
res = _(json1).concat(json2).groupBy('id').map(_.spread(_.assign)).value();
in ES2015:
res = json2.map(x => Object.assign(x, json1.find(y => y.id == x.id)));
ES2015 georg's answer works great;
json1 = [
{id:1, test: 0},
{id:2, test: 0},
{id:3, test: 0},
{id:4, test: 0},
{id:5, test: 0}
];
json2 = [
{id:1, test: 1},
{id:3, test: 1},
{id:5, test: 1}
];
json1.map(x => Object.assign(x, json2.find(y => y.id == x.id)));
result:
{id:1, test: 1},
{id:2, test: 0},
{id:3, test: 1},
{id:4, test: 0},
{id:5, test: 1}