Similar questions can be found here and here, but I am struggling to modify the code provided in the answers to fit my specific needs due to its conciseness.
The structure of my array of objects is as follows:
[
{
id: 1,
someProp: "value of some prop",
metaData: {
metaId: 123
uuid: "2348_4354_dfgg_r343"
}
},
{
id: 2,
someProp: "value of some prop again",
metaData: {
metaId: 321
uuid: "9897_3543_ergl_j435"
}
}
]
I need to transform it into the following structure:
[
{
id: 1,
someProp: "value of some prop",
metaId: 123
uuid: "2348_4354_dfgg_r343"
},
{
id: 2,
someProp: "value of some prop again",
metaId: 321
uuid: "9897_3543_ergl_j435"
}
]
The goal is to merge the properties from the nested metaData
object into the parent object for each item in the array. I have attempted the following code:
console.log(Object.assign(
{},
...function _flatten(o) {
return [].concat(...Object.keys(o)
.map(k =>
typeof o[k] === 'object' ?
_flatten(o[k]) :
({[k]: o[k]})
)
);
}(events)
));
However, this code only creates one flattened object, whereas I need to flatten multiple objects in the array.