Reputation: 25
I have a array of objects data in
const data = [
{ total: 9, country: 'IN' },
{ total: 0, country: 'AF' ]};
But all i need to convert above data into countries Map(plugin) format
data = {IN: { value: 9 },AF: { value: 0}};
Is there any possibilities converting into this format.
Please help me in these issue.
Thanks in Advance
Upvotes: 0
Views: 118
Reputation: 97182
Just reduce()
to an object:
const data = [
{ total: 9, country: 'IN' },
{ total: 0, country: 'AF' }
];
const result = data.reduce((a, {country, total}) => ({
...a,
[country]: {value: total}
}), {});
console.log(result);
Upvotes: 1