Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Filter multiple specific properties from an array of objects into a new array

I have a big array of objects such as

let arr = [
  {name: "name1" , surname: "surname1", age: "22"},
  {name: "name2" , surname: "surname2", age: "23"},
  {name: "name3" , surname: "surname3", age: "24"},
]

But i need to filter it so it will be

arr = [
  {name: "name1" , surname: "surname1"},
  {name: "name2" , surname: "surname2"},
  {name: "name3" , surname: "surname3"},
]

How can i do so?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Using Array#map:

const arr = [ {name: "name1" , surname: "surname1", age: "22"}, {name: "name2" , surname: "surname2", age: "23"}, {name: "name3" , surname: "surname3", age: "24"} ];

const res = arr.map(({ name, surname }) => ({ name, surname }));

console.log(res)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading