I have complex json like this:
const lstCountry = {
"cols": ["id", "countryName", "area"],
"data": [
["1", "Russia", "17098242"],
["2", "Canada", "9970610"],
["3", "China", "9640821"],
["4", "United States", "9629091"],
["5", "Brazil", "8514877"],
["6", "Australia", "7741220"],
["7", "India", "3287263"],
["8", "Argentina", "2780400"],
["9", "Kazakhstan", "2724900"],
["10", "Sudan", "2505813"]
]
};
Now i want to convert into simple array as a output like this :
const countryList = [
{ "id": "1", "countryName": "Russia", "area": "17098242" },
{ "id": "2", "countryName": "Canada", "area": "9970610" },
{ "id": "3", "countryName": "China", "area": "9640821" },
{ "id": "4", "countryName": "United States", "area": "9629091" },
{ "id": "5", "countryName": "Brazil", "area": "8514877" },
{ "id": "6", "countryName": "Australia", "area": "7741220" },
{ "id": "7", "countryName": "India", "area": "3287263" },
{ "id": "8", "countryName": "Argentina", "area": "2780400" },
{ "id": "9", "countryName": "Kazakhstan", "area": "2724900" },
{ "id": "10", "countryName": "Sudan", "area": "2505813" },
];
I have already tried below but got output using index as a key, So how can i convert that key with cols array as a key and another data array as a value and generate my own array with these key and value combination.
Or Is there any easy way to archive this like using map, reduce?
Already tried this :
let mainData = [];
for (let i = 1; i < Object.keys(data).length; i++) {
const eleData = data[Object.keys(data)[i]];
for (let j = 0; j < eleData.length; j++) {
const element = Object.assign({}, eleData[j]);
mainData.push(element);
};
};
console.log('mainData:', mainData)
Tried function output:
const mainData = [
{ "0": "1", "1": "Russia", "2": "17098242" },
{ "0": "2", "1": "Canada", "2": "9970610" },
{ "0": "3", "1": "China", "2": "9640821" },
{ "0": "4", "1": "United States", "2": "9629091" },
{ "0": "5", "1": "Brazil", "2": "8514877" },
{ "0": "6", "1": "Australia", "2": "7741220" },
{ "0": "7", "1": "India", "2": "3287263" },
{ "0": "8", "1": "Argentina", "2": "2780400" },
{ "0": "9", "1": "Kazakhstan", "2": "2724900" },
{ "0": "10", "1": "Sudan", "2": "2505813" },
];
