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

138
Views
How to turn an Array into an Object. Elements of the Array should be Keys and the Values should be output from the Callback

I am trying to code to the function arrToObj to achieve the desired console logs. arrToObj should return an object that has elements from the passed-in array as keys, and the outputs from the callback (when those elements are passed in) as the corresponding values.

Here is the code I am given...

function arrToObj(array, callback) {
  // ADD CODE HERE
}

// Uncomment these to check your work!
 const arrOfStrings = ['beer', 'glue'];
 const capitalize = str => str.toUpperCase();
 console.log(arrToObj(arrToObj(arrOfStrings, capitalize)); // should log: { beer: 'BEER', glue: 'GLUE' }

I'm not sure where to start. Hints or solutions are both welcome. Thank you for your time!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can just use reduce

function arrToObj(array, callback) {
  return array.reduce((acc, next) => {
    return {
      ...acc,
      [next]: callback(next),
    };
  }, {})
}

// Uncomment these to check your work!
const arrOfStrings = ['beer', 'glue'];
const capitalize = str => str.toUpperCase();
console.log(arrToObj(arrOfStrings, capitalize)); // should log: { beer: 'BEER', glue: 'GLUE' }

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!