I have an array like this
var data = [
{
family: "Fam A",
category: "Cat A",
products: [
{
name: "Name A1",
style: "Style A1"
},
{
name: "Name A2",
style: "Style A2"
}
]
},
{
family: "Fam B",
category: "Cat B",
products: [
{
name: "Name B1",
style: "Style B1"
},
{
name: "Name B2",
style: "Style B2"
}
]
}
];
I want to change the inside objects like below
var data = [
{family:"Fam A", category: "Cat A", name: "Name A1", style:"Style A1"},
{family:"Fam A", category: "Cat A", name: "Name A2", style:"Style A2"},
{family:"Fam B", category: "Cat B", name: "Name B1", style:"Style B1"},
{family:"Fam B", category: "Cat B", name: "Name B2", style:"Style B2"}
]
I've tried using map and forEach that I found in other posts but haven't gotten anything to work.
var flattened = data.products.map(x => Object.assign(x, { productDetails: data.products }))
function getList(data, arrayKey, arrayName) {
data[arrayKey].forEach(function(element) {
element[arrayName] = data[arrayName];
});
return data[arrayKey];
}
The ideal solution would be able to handle dynamic no of nestings but not necessary to move on.
Thank you in advance for helping a newer dev!