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

164
Views
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 answers
Answer question

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 Report

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 Report

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 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!