I'm attempting to convert an array of objects into a dictionary using TypeScript. Below is the code I have written:
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})));
The output that I am currently getting is:
{1: "Germany", 2: "Austria", 3: "Switzerland"}
I want to include the population in the output as well. When I try to modify the code, it results in a syntax error:
let dictionary = Object.assign({}, ...data.map((x) => ({[x.id]: x.country, x.population})));
The desired output should resemble the following:
{
"1": {
"country": "Germany",
"population": 83623528
},
"2": {
"country": "Austria",
"population": 8975552
},
"3": {
"country": "Switzerland",
"population": 8616571
}
}