Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

177
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda