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

181
Views
Using Array.map(), to extract values from array of key:value objects

I have been able to use the following code to extract values for a list of specific keys from an array of objects.

let keys = ['name', 'age'];
let employees = [
  {name: 'Matt', employeeID: 123, performance: 'good', age: 21},
  {name: 'Brian', employeeID: 321, performance: 'average', age: 32},
  {name: 'David', employeeID: 456, performance: 'best', age: 35},
  {name: 'Kevin', employeeID: 654, performance: 'poor', age: 38},
  {name: 'Barrack', employeeID: 789, performance: 'super', age: 47},

];

let extracted = employees.map(object => keys.map(element => object[element]));
console.log(extracted); //result is [[Matt, 21.0], [Brian, 32.0], [David, 35.0], [Kevin, 38.0], [Barrack, 47.0]]

I have a hard time understanding conceptually how using two Array.map() methods nested within each other works like this. Can somebody walk me through how this actually works? For instance if I want to use only one Array.map() method to recreate the above, I end up with an array of truncated objects that still includes key:value pairs.

let truncated = employees.map(({name, age, ...rest}) => ({name, age}));
console.log(truncated); //result is [{age=21.0, name=Matt}, {age=32.0, name=Brian}, {age=35.0, name=David}, {name=Kevin, age=38.0}, {age=47.0, name=Barrack}]
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

On the first iteration of employees.map(), object is the object

{name: 'Matt', employeeID: 123, performance: 'good', age: 21}

We then execute keys.map().

On the first iteration, element is 'name', and we return object[element], which is 'Matt'. On the second iteration, element is 'age', and we return object[element], which is 21. map() combines these results into the array ['Matt', 21].

We then repeat the above for each object in the employees array, and make a list of all these results. This produces an array of arrays.

about 4 years ago · Juan Pablo Isaza Report

0

You end up with a nested array because both maps return arrays. Your "inner" map returns an array of values based on the keys array, and that is returned to the "outer" map that's been iterating over the array of each object, which also returns an array.

// Create a new array by mapping over each object
employees.map(object => {
  
  // For each key in the keys array return a
  // new array of only those values of the
  // properties of the object that match the key
  return keys.map(element => object[element]);
});

about 4 years ago · Juan Pablo Isaza Report

0

Array map takes an array of elements and transforms each element using the supplied function -

// input
[1,3,5,7].map(a => a + 10)
input a => a + 10 output
1 1 + 10 11
3 3 + 10 13
5 5 + 10 15
7 7 + 10 17
// output
[11,13,15,17]

Now we look at it with a more simplified view -

// in       // out
[           [ 
  1,  // =>   11,
  3,  // =>   13,
  5,  // =>   15,
  7,  // =>   17
]           ]

And now expand it with a level of nesting using your objects -

[
  {
    name: 'Matt',          // [            [
    employeeID: 123,       //   "name", =>   "Matt",
    performance: 'good',   //   "age"        21
    age: 21                // ]            ]
  },
  {
    name: 'Brian',          // [            [
    employeeID: 321,        //  "name", =>    "Brian",
    performance: 'average', //  "age"         32
    age: 32                 // ]            ]
  },
  {
    name: 'David',          // [            [
    employeeID: 456,        //  "name", =>    "David",
    performance: 'best',    //  "age"         35
    age: 35                 // ]            ]
  },
  {
    name: 'Kevin',          // [            [
    employeeID: 654,        //  "name", =>    "Kevin",
    performance: 'poor',    //  "age"         38
    age: 38                 // ]            ]
  },
  {
    name: 'Barrack',        // [            [
    employeeID: 789,        //  "name", =>    "Barrack",
    performance: 'super',   //  "age"         47
    age: 47                 // ]            ]
  }
]
[
  [
    "Matt",
    21
  ],
  [
    "Brian",
    32
  ],
  [
    "David",
    35
  ],
  [
    "Kevin",
    38
  ],
  [
    "Barrack",
    47
  ]
]
[
  ["Matt",21],
  ["Brian",32],
  ["David",35],
  ["Kevin",38],
  ["Barrack",47]
]
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!