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

165
Vistas
In Javascript, given key/value pairs, return values from array of keys
const pairs = { a : 1, b : 2, c : 3 }
const keyArray = [a, b, c]

I'm trying to get a function or w/e that returns [1, 2, 3]. can't think of anything please help

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

In javascript all keys of an object are actually strings, and can be addressed using the same strings.

Your code is creating an array containing the values of the - possibly uninitialized - variables a, b and c, which then produces [undefined, undefined, undefined], or else whatever those variables contain.

You need to make keyArray contain strings instead, and then you can use the map() function to produce the desired result:

const pairs = { "a" : 1, "b" : 2, "c": 3 }
const keyArray = ["a", "b", "c"]
const values = keyArray.map(key => pairs[key]);
console.log(values);

about 4 years ago · Juan Pablo Isaza Denunciar

0

const pairs = { 'a' : 1, 'b' : 2, 'c': 3 }

for(const [key , value] of Object.entries(pairs))
{
    console.log(key) // a , b, c
    console.log(value) // 1, 2, 3
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

const pairs = { 'a' : 1, 'b' : 2, 'c': 3 }
const keyArray = ['a', 'b', 'c']

for(const item of keyArray)
{
    console.log(pairs[item])
}

You need to make your items inside the array and object as strings

about 4 years ago · Juan Pablo Isaza 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