Skip to content
Advertisement

Merge objects with same id in array in js

I guess have a dead simple problem but still didn’t find a solution I have an array which looks like this:

JavaScript

I’d like to modify it to look like this (merge by id and join elements):

JavaScript

I already had multiple tries but still didn’t find an elegant way of doing it.

Advertisement

Answer

You can group the array by pids and merge the non null coordinates using reduce.

JavaScript

Relevant Documentations:

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement