I have an array of objects that has one of the properties("attributes"
) as array of objects.
var products = [
{
"productId": "1",
"attributes": [
{
"variant":"red",
"price": "134.00"
}
]
},
{
"productId": "2",
"attributes": [
{
"variant": "green",
"value": "3400.00"
},
{
"variant": "pink",
"price": "342.00"
}
]
}
]
I want the nested array of objects to be flattened and copied onto parent but the parent object needs to be duplicated for each of the nested object. (Not sure if I have explained it correctly).
The converted array of objects should be:
var transformedProducts = [
{
"productId": "1",
"variant":"red",
"price": "134.00"
},
{
"productId": "2",
"variant": "green",
"value": "3400.00"
},
{
"productId": "2",
"variant": "pink",
"price": "342.00"
}
]
I can map over the outer array and then again map over the inner array, and in the inner most map, construct a new object.
Is there a better or more functional approach to this?