Skip to content
Advertisement

Iterate nested array items and, upon the same value of a specific key, collect any other entry value as data of a merger which is grouped by key+value

I have an array that looks like this:

enter image description here

So as a strucutre it would be something like:

JavaScript

This wierd array of objects happens because at some point, in our app, we are creating an array of reasons to object something using the same object.

I need to be able to merge the nested array of objects to look like this:

JavaScript

But I’ve been struggling with this for some days, looking for some lodash functions but still no luck…

I’m completely lost on how to achieve this. All examples look simpler, with less nested arrays. Any idea on how to merge all props for the same object key?

Thanks a lot in advance.

Advertisement

Answer

If I’m not missing something, you are looking for this:?

JavaScript

like:

JavaScript

Another way:

JavaScript

update:

Yes, I DO HAVE MISSED SOMETHING. Thanks @PeterSeliger for pointing it out. But instead of delete this answer, I’d like to update and correct it.

The code below is NOT TESTED.

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