Within my possession are two arrays:
var g= [
{ id: 36, name: 'AAA', goal: 'yes' },
{ id: 40, name: 'BBB', goal: 'yes' },
{ id: 39, name: 'JJJ', goal: 'yes' },
{ id: 27, name: 'CCC', goal: 'yes' }];
var c= [
{ id: 36, color:"purple" },
{ id: 40, color:"purple" },
{ id: 100, color:"pink"} ];
I am seeking the following output (performing a left join based on 'id'):
res = [{ id: 36, name: 'AAA', goal: 'yes' , color:"purple"}, { id: 40, name: 'BBB', goal: 'yes', color:"purple" }]
The current logic I have performs a merge, but now I am in need of a logic for a left join:
function mergeBy(key, data) {
return Array.from(data
.reduce((m, o) => m.set(o[key], { ...m.get(o[key]), ...o }), new Map)
.values()
);
}