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

javascript - Build an array of objects from another array of object by selecting just a part of its properties - Stack Overflow

programmeradmin3浏览0评论

Having the following input array:

const initialArray = [{name: 'john', age: 12, height: 178, likes: 'music'},
                      {name: 'mike', age: 22, height: 181, likes: 'sport'},
                      {name: 'anna', age: 18, height: 175, likes: 'sleep'},
                      {name: 'paul', age: 24, height: 170, likes: 'drink'}
                     ];

I want to build a new array of objects that contains only some properties, for example, just name and height. The result would be:

result = [{name: 'john', height: 178},
          {name: 'mike', height: 181},
          {name: 'anna', height: 175},
          {name: 'paul', height: 170}
         ];

Tried with map but probably something is wrong:

  const result = initialArray.map((a) => {
    a.name, a.height
  });

Having the following input array:

const initialArray = [{name: 'john', age: 12, height: 178, likes: 'music'},
                      {name: 'mike', age: 22, height: 181, likes: 'sport'},
                      {name: 'anna', age: 18, height: 175, likes: 'sleep'},
                      {name: 'paul', age: 24, height: 170, likes: 'drink'}
                     ];

I want to build a new array of objects that contains only some properties, for example, just name and height. The result would be:

result = [{name: 'john', height: 178},
          {name: 'mike', height: 181},
          {name: 'anna', height: 175},
          {name: 'paul', height: 170}
         ];

Tried with map but probably something is wrong:

  const result = initialArray.map((a) => {
    a.name, a.height
  });
Share Improve this question asked May 14, 2021 at 9:34 Jean PierreJean Pierre 3211 gold badge9 silver badges24 bronze badges 1
  • try this initialArray.map((a) => ({name: a.name, height: a.height})); – Krzysztof Krzeszewski Commented May 14, 2021 at 9:37
Add a ment  | 

4 Answers 4

Reset to default 7

You should either use return keyword inside {} or warp the code with () (parenthesis).

You can try using Destructuring assignment which allow you to specify the property names you want in the resulting objects:

const initialArray = [
  {name: 'john', age: 12, height: 178, likes: 'music'},
  {name: 'mike', age: 22, height: 181, likes: 'sport'},
  {name: 'anna', age: 18, height: 175, likes: 'sleep'},
  {name: 'paul', age: 24, height: 170, likes: 'drink'}
];
const result = initialArray.map(({name, height}) => ({name,height}));
console.log(result);

you should return the object like below.

const result = initialArray.map((a) => {
  return {name:a.name, height:a.height}
});

You can easily achieve this using map, reduce or for..of loop

1) Using map

const initialArray = [
  { name: "john", age: 12, height: 178, likes: "music" },
  { name: "mike", age: 22, height: 181, likes: "sport" },
  { name: "anna", age: 18, height: 175, likes: "sleep" },
  { name: "paul", age: 24, height: 170, likes: "drink" },
];

const result = initialArray.map(({ name, height }) => ({ name, height }));
console.log(result);

2) Using reduce

const initialArray = [
  { name: "john", age: 12, height: 178, likes: "music" },
  { name: "mike", age: 22, height: 181, likes: "sport" },
  { name: "anna", age: 18, height: 175, likes: "sleep" },
  { name: "paul", age: 24, height: 170, likes: "drink" },
];

const result = initialArray.reduce((acc, { name, height }) => {
  acc.push({ name, height });
  return acc;
}, []);
console.log(result);

3) Using for..of loop

const initialArray = [
  { name: "john", age: 12, height: 178, likes: "music" },
  { name: "mike", age: 22, height: 181, likes: "sport" },
  { name: "anna", age: 18, height: 175, likes: "sleep" },
  { name: "paul", age: 24, height: 170, likes: "drink" },
];

const result = [];
for (let { name, height } of initialArray) {
  result.push({ name, height });
}
console.log(result);

Another way would be with lodash's pick (if you need to dynamically resolve the name of the properties) or ramda's pick methods are doing exactly this

const result = initialArray.map(R.pick(['name', 'height']));

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论