JavaScript: Map an array of objects to a dictionary - DEV Community

PHOTO EMBED

Tue Nov 16 2021 15:05:02 GMT+0000 (UTC)

Saved by @arielvol

let data = [
  {id: 1, country: 'Germany', population: 83623528},
  {id: 2, country: 'Austria', population: 8975552},
  {id: 3, country: 'Switzerland', population: 8616571}
];

let dictionary = Object.assign({}, ...data.map((x) => ({[x.id]: x.country})));
// {1: "Germany", 2: "Austria", 3: "Switzerland"}
content_copyCOPY

https://dev.to/devtronic/javascript-map-an-array-of-objects-to-a-dictionary-3f42