In my JavaScript code, I am attempting to extract the Category property as an array from a nested key/value object structure. Here is an example of the data:
var data = [{key: "A", values:[{Category:"One", amount:2000},
{Category: "Two", amount:2500},
{Category: "Three", amount: 3000},
{Category: "Four", amount: 3000}]
},
{key: "B", values:[{Category:"One", amount:2000},
{Category: "Two", amount:2500},
{Category: "Three", amount: 3000},
{Category: "Four", amount: 3000}]
},
{key: "C", values:[{Category:"One", amount:2000},
{Category: "Two", amount:2500},
{Category: "Three", amount: 3000},
{Category: "Four", amount: 3000}]
}]
The desired output is to have an array like this:
["One","Two","Three","Four"]
I have tried various methods to achieve this goal, with one attempt using a nested map function shown below. The variable data represents the above JavaScript object.
x = data.map(function(el) {return el.values.map(function(ele,i){return
ele.Category;})})
However, the current result is an array of arrays instead of a single array. Although I can manipulate the output to get the desired result, I believe there might be a more efficient solution.
[["One","Two","Three","Four"],["One","Two","Three","Four"],
["One","Two","Three","Four"]]
If you have any suggestions or alternative approaches, they would be greatly appreciated. Thank you!