0

How can I add one key:value pair to object in list where id match between I want to add uuid to list2 where id between list 1 and 2 are the same.

I think I need to combine filter and map, but I cannot work it out. Any suggestions?

list 1

[ { uuid: 26, id: 317 }
  { uuid: 21, id: 451 },
  { uuid: 43, id: 504 }, ]

list 2

[ { id: 317, coords: [ 33.4325301346224, 12.3742694854736 ] },
  { id: 451, coords: [ 43.4257858672581, 14.4208612450748 ] },
  { id: 504, coords: [ 33.4225091429188, 12.395770072937 ] } ]

desired result

[ { id: 317, coords: [ 33.4325301346224, 12.3742694854736 ], uuid:26},
      { id: 451, coords: [ 43.4257858672581, 14.4208612450748 ], uuid:21 },
      { id: 504, coords: [ 33.4225091429188, 12.395770072937 ], uuid:43 } ]
geogrow
  • 485
  • 2
  • 9
  • 26

1 Answers1

1

Here it is, in case of any question don't hesitate to ask.

list2.reduce((list, entry) => {
    const commonElement = list1.find(_entry => _entry.id === entry.id);

    if (commonElement) {
        list.push({
            ...entry,
            ...commonElement,
        });
    } else {
        list.push(entry);
    }

    return list;
}, []);
Nubzor
  • 522
  • 1
  • 5
  • 14
  • Thanks I will try that too! I managed to solve it from using an example from the recommended threads – geogrow Mar 08 '19 at 14:32