Sai Charan
Sai Charan

Reputation: 25

How can i convert array of objects data into a Countries-Map data format?

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

Answers (1)

Robby Cornelissen
Robby Cornelissen

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

Related Questions