Skip to content
Advertisement

unify objects from array to a bigger object

My english is not really good and it is hard to explain what I want in the title, maybe this will help: I have some small data objects which I get from answers of a form. Something like this:

JavaScript

I pushed them into an array, so I get this:

JavaScript

I need to unify this so the objects with same key (i.e. 3) would merge into one, so I get:

JavaScript

I can’t change the data structure , so this is what I came up so far which seems to work:

JavaScript

but this is really complicated and hard to read. So I want to know if someone knows a better/simpler way to do this?

Advertisement

Answer

You can try with this code:

JavaScript
Advertisement