Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

164
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda