Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

235
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda