Mapping through an array to produce an object

Issue

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?

Solution

Can do it:

let dates = [
  "2022-05-20",
  "2022- 06-22",
  "2022-06-20"
];

let newObj = Object.assign(...dates.map(key => ({[key]: {disabled: true}})));

console.log(newObj)

Answered By – Bahador Raghibizadeh

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published