DEV Community

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

 
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 y.name===x.name

Thread Thread
 
nicolege profile image
nicole-ge

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
fetishlace

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
arr.map(x=>x.series)