最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Mapping an array of objects to dictionary in typescript - Stack Overflow

programmeradmin3浏览0评论

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
Add a ment  | 

3 Answers 3

Reset to default 6

I 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)

发布评论

评论列表(0)

  1. 暂无评论