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

157
Views
Mapping through an array to produce an object

I have an array :

[
"2022-05-20",
"2022- 06-22",
"2022-06-20"
]

and I want to produce an object like this:

{
    '2022-05-20': {disabled:true},
    '2022-06-22': {disabled: true},
'2022-06-20': {disabled: true},
  }

I tried using a for loop but it kept producing errors. Is this possible with javascript?

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

0

You can use Array#reduce as in the following demo. You can also use Array#map but you would have to use Object.fromEntries as well.

const input = [ "2022-05-20", "2022- 06-22", "2022-06-20" ],

      output = input.reduce(
          (prev,cur) => 
          ({...prev,[cur]:{disabled:true}}), {}
      );
      
      
console.log( output );

USING Array#map ...

Here is how you can use Array#map:

const input = [ "2022-05-20", "2022- 06-22", "2022-06-20" ],

      output = Object.fromEntries(
          input.map(date => [date, {disabled:true}])
      );
      
      
console.log( output );

about 4 years ago · Juan Pablo Isaza Report

0

This might get the job done.

const yourArray = ["2022-05-20", "2022-06-22", "2022-06-20"];
const obj = {};
for(const x of yourArray) obj[String(x)] = { disabled: true };
console.log(obj); // :)

Create the variable obj that is going to save the produced object you want. Iterating throw your array and using a string parsed version of the value in the current iteration (parsing just in case, if you already know the array is made of strings, this is kinda unnecessary) to save it as a key on the new object, also assigning to that key, the value { disabled: true }.

about 4 years ago · Juan Pablo Isaza Report

0

Here is a one liner solution:

let res = data.reduce((acc, curr) =>(acc[curr] = {disabled: true}, acc), {});
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!