I am trying to map array of objects to dictionary using typescript. I have written the following code:
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})));
I am getting output as below:
{1: "Germany", 2: "Austria", 3: "Switzerland"}
I want to get population in the output as well and for that i am making the following code change but it is giving syntax error:
let dictionary = Object.assign({}, ...data.map((x) => ({[x.id]: x.country, x.population})));
Desired output is similar to below:
{
"1": {
"country": "Germany",
"population": 83623528
},
"2": {
"country": "Austria",
"population": 8975552
},
"3": {
"country": "Switzerland",
"population": 8616571
}
}
I am trying to map array of objects to dictionary using typescript. I have written the following code:
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})));
I am getting output as below:
{1: "Germany", 2: "Austria", 3: "Switzerland"}
I want to get population in the output as well and for that i am making the following code change but it is giving syntax error:
let dictionary = Object.assign({}, ...data.map((x) => ({[x.id]: x.country, x.population})));
Desired output is similar to below:
{
"1": {
"country": "Germany",
"population": 83623528
},
"2": {
"country": "Austria",
"population": 8975552
},
"3": {
"country": "Switzerland",
"population": 8616571
}
}
Share
Improve this question
edited Apr 23, 2020 at 4:41
meallhour
asked Apr 23, 2020 at 4:28
meallhourmeallhour
15.6k24 gold badges72 silver badges151 bronze badges
3
- 1 What is your expected output ? - the one you pasted is not a valid javascript object – mickl Commented Apr 23, 2020 at 4:29
- You're missing a key, and expected output is not a valid object – Code Maniac Commented Apr 23, 2020 at 4:31
-
Your
Desired output
as mentioned in the question is syntactically wrong. – vatz88 Commented Apr 23, 2020 at 4:35
3 Answers
Reset to default 6I think you are hopping something like this:
let data = [
{id: 1, country: 'Germany', population: 83623528},
{id: 2, country: 'Austria', population: 8975552},
{id: 3, country: 'Switzerland', population: 8616571}
];
let dictionary = Object.fromEntries(data.map(item => [item.id, {country: item.country, population: item.population}]));
console.log(dictionary);
You can try using Object.fromEntries
(assuming your value needs to be an object to preserve both country
and population
):
let data = [
{id: 1, country: 'Germany', population: 83623528},
{id: 2, country: 'Austria', population: 8975552},
{id: 3, country: 'Switzerland', population: 8616571}
];
let dictionary = Object.fromEntries(data.map(({id,...rest})=> ([id, rest]) ));
console.log(dictionary);
or in case you want to return an array with no keys:
let data = [
{id: 1, country: 'Germany', population: 83623528},
{id: 2, country: 'Austria', population: 8975552},
{id: 3, country: 'Switzerland', population: 8616571}
];
let dictionary = Object.fromEntries(data.map(({id,...rest})=> ([id, Object.values(rest)]) ));
console.log(dictionary);
You're almost there, you need to build a object for id
, and use rest parameter
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(({
id,
...rest
}) => ({
[id]: rest
})));
console.log(dictionary)