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

232
Views
How can I replace array item with matched object keys?

Let's say I have an array of id's const ids = [[1, 2, 3], [2, 3], [3]]

And have array with objects that have name for each id

const obj = [
    { id: 1, name: "One" },
    { id: 2, name: "Two" },
    { id: 3, name: "Three" },
  ];

What is the most proper way to get ids = [["One", "Two", "Three"], ["Two", "Three"], ["Three"]], I'm worrying that nested mapping could cause performance issues.

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Use a combination of map and find. There may be a more performant way of doing it, but I'd suggest worrying about that only if you run into performance issues :-

const ids = [[1, 2, 3], [2, 3], [3]];

const obj = [
    { id: 1, name: "One" },
    { id: 2, name: "Two" },
    { id: 3, name: "Three" },
  ];

const mapped = ids.map(arr => arr.map(id => obj.find(obj => obj.id === id).name));
about 4 years ago · Santiago Trujillo Report

0

const obj = [
    { id: 1, name: "One" },
    { id: 2, name: "Two" },
    { id: 3, name: "Three" },
  ];

const ids = [[1, 2, 3], [2, 3], [3]] ;

// dedicated object to keep association between id and names
let names = {} ;
obj.forEach( o => {
    names[ o.id ] = o.name ;
} ) ;

// map each sub array content to their real name
for( let i = 0; i <= ids.length-1; i++){
    ids[i] = ids[i].map( id => names[id] ) ;
}

console.log( ids ) ;

=>

[ [ 'One', 'Two', 'Three' ], [ 'Two', 'Three' ], [ 'Three' ] ]
about 4 years ago · Santiago Trujillo 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!