DEV Community


Discussion on: How to convert an array into an object in javascript

nicolege profile image

Thank you this works very good.

I have one question:
what does arr.forEach(x=>result.find(y=>;

I don't understand this function. I left it out and works. What does it?

Thread Thread
fetishlace profile image
fetishlace • Edited

It cannot work without it, it makes no sense. I've just generated array with unique names and empty series array at first (result), this one is pushing all the series objects data into these empty series arrays in result array.

It's just going through original array (arr) to pick series object and push it in result series array - names properties has to match hence

Thread Thread
nicolege profile image

to understand the code a bit better, how I have to adjust it to get the following result:

{"name":"Area 1","value":3},
{"name":"Area 2","value":3},
{"name":"Area 2","value":1}

Thread Thread
fetishlace profile image

From that?
let arr = [
{"name":"Product 1","series":{"name":"Area 1","value":3}},
{"name":"Product 2","series":{"name":"Area 2","value":3}},
{"name":"Product 1","series":{"name":"Area 2","value":1}},
U can just remap it>x.series)