2

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
  });
1
  • try this initialArray.map((a) => ({name: a.name, height: a.height})); Commented May 14, 2021 at 9:37

4 Answers 4

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

Sign up to request clarification or add additional context in comments.

Comments

4

you should return the object like below.

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

Comments

3

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

Comments

1

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']));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.