I have a scenario while working for charts where i need to convert the below array of objects into other array of arrays.
Inputs
const country = [
{"country": "Germany","visits": "306"},
{"country": "USA","visits": "106"},
{"country": "UK","visits": "206"},
];
and the desired output should look like below::
[
["Country", "Visits"],
["Germany", 306],
["USA", 106],
["UK", 206]
]
I am unable to get this desired output.
mapon array to transform them:const output = country.map(c => [c.country, c.visits])country[0]for example.const country = [ { country: 'Germany', visits: '306' }, { country: 'USA', visits: '106' }, { country: 'UK', visits: '206' }, ]; const reducer = (acc, curr, idx, self) => { const result = acc; result.push([curr.country, curr.visits]) if (idx === self.length - 1) { const keys = Object.keys(curr); result.unshift(keys); } return result; } const result = country.reduce(reducer, []) console.log(result);