I have the below set of data that is in the format of objects within an array.
const columns = [
{
"usage": 395226,
"population": 1925117,
"value": 21,
"abv": "MN"
},
{
"usage": 893129,
"population": 4327541,
"value": 21,
"abv": "IL"
}
]
I require the above set of data to be converted into below shown format. I have been stuck here for a while. I tried writing a function for converting this into the desired data format, but it didn't work.
let columns = [
["abv", "population"],
["MN", 1925117],
["IL", 4327541]
]
It would be a great help if anyone could help me solve this. Thanks in advance :)
I would do it with a reducer:
const cols = [
{
"usage": 395226,
"population": 1925117,
"value": 21,
"abv": "MN"
},
{
"usage": 893129,
"population": 4327541,
"value": 21,
"abv": "IL"
}
]
cols.reduce(function(acc, current) {
acc.push([current.abv, current.population])
return acc;
}, ["abv", "population"]);
Or if you don't like using push
:
cols.reduce(function(acc, current) {
acc = [
...acc,
[current.abv, current.population]
]
return acc;
}, ["abv", "population"]);