I have an array like so with each array item being an object and there can be multiple objects:
originalArray = [
{
"category": "ASDGFVFG",
"total": 1
},
{
"category": "DFGH",
"total": 1
},
.........
]
How can I convert it into the following structure? Note that categoryAndValueItems is fixed, so it can be 'hard-coded'.
newData = [{
categoryAndValueItems: [
{
"category": "ASDGFVFG",
"total": 1
},
{
"category": "DFGH",
"total": 1
},
.........
]
}]
I know that I can use .map to create a new array using values in an original array like so:
newArray = originalArray.map(x => ({....}))
I also know that I can use the dot notation or the square bracket notation to create a new key:
obj["categoryAndValueItems"] = ...
However, I'm unsure of how to go further or combine the two, how would I be able to convert the array?
categoryAndValueItemsproperty, and this property contains the old data?