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

115
Views
How to convert array to an object with key/value pairs Javascript

I'm trying to have a configurable checkbox and want to have my array's values to be the keys for the object.

Given:

let colors = [red, blue, black, yellow, orange]

How do I make it so that it becomes:

colorChecklist = {
    red: true,
    blue: true,
    black: true,
    yellow: true,
    orange: true
}
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

const obj = {};
colors.forEach(c => obj[c] = true)

I know everybody likes reduce but it's unnecessarily complicated and well, see this and this

about 4 years ago · Juan Pablo Isaza Report

0

You can use reduce here .

One-liner

const colorChecklist = colors.reduce((acc, curr) => (acc[curr] = true, acc), {});

let colors = ["red", "blue", "black", "yellow", "orange"];

const colorChecklist = colors.reduce((acc, curr) => {
  acc[curr] = true;
  return acc;
}, {});

console.log(colorChecklist);

about 4 years ago · Juan Pablo Isaza Report

0

Just use Array.prototype.reduce

const [red, blue, black, yellow, orange] = ["red", "blue", "black", "yellow", "orange"]

let colors = [red, blue, black, yellow, orange];

let colorChecklist = colors.reduce((acc, key) => (acc[key] = true, acc), {});

console.log(colorChecklist);

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!