Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

119
Views
pick multiple keys and values from array of objects

I need to pick few keys and values (only name and age) from below array of objects.

const studentList = [{"id": "1", "name": "s1". "age": 10, "gender" : "m", "subject": "Maths"},{"id": "2", "name": "s2". "age": 11, "gender" : "m", "subject": "Maths"}]

I can achieve that using map and lodash pick as in below.

let nameAndAgeList = studentList.map(student => pick(student, ["name", "age"]));

But is there any more easy way with only using map. I know we can retrieve only one property as in below.

let nameArr = (studentList).map(({ name }) => name);

But how can we retrieve both name and age using map? Or any more easy & best ways with es6+

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

The studentList assignment is not valid (it contains dots instead of comma). Corrected in the snippet. You can map name and age in one go using:

const studentList = [
  {"id": "1", "name": "s1", "age": 10, "gender" : "m", "subject": "Maths"},
  {"id": "2", "name": "s2", "age": 11, "gender" : "m", "subject": "Maths"}];
console.log( studentList.map( ({name, age}) => ({name, age}) ) );

about 4 years ago · Juan Pablo Isaza Report

0

You can retrieve multiple properties, just like in the example below.

const studentList = [{"id": "1", "name": "s1", "age": 10, "gender" : "m", "subject": "Maths"},{"id": "2", "name": "s2", "age": 11, "gender" : "m", "subject": "Maths"}]

let nameArr = (studentList).map(({ name, age }) => {
  return {'name': name, 'age': age}
});

console.log(nameArr)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!