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

78
Views
Push only certain items from array of objects to new array of arrays

Hello so i have array of objects something like this

const old = [{id: 1, name: 'Random'}, {id: 2, name: 'Random2'}]

also i have array

const wantedField = ['id']

So looking this result, i need values only for certain key

const finally = [[1],[2]]

I have tried something like this but not luck. End result should be array of arrays with just certain values.

    old.map((obj, value) => {
          const key = Object.keys(obj)[value]
          if(wantedField.includes(const)) {
          const newArray = []
          const key = Object.keys(obj)[value]
          newArray.push(obj[key])
          return [newArray]
       }
   })

So this return [newArray] is wrong should return multiple values not just one. Please help regards.

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

0

This is a one-liner, with two nested maps. One to iterate over the input array, and another to iterate over the wanted field(s):

const old = [{id: 1, name: 'Random'}, {id: 2, name: 'Random2'}];
const wantedField = ['id'];

const result = old.map(o => wantedField.map(k => o[k]));
console.log(result);

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!