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

244
Vistas
How can I spread array elements into an object as the keys, and define some value for them?

Let say I have an array:

const arr = ['a',  'b, 'c'];

I want to create an object like this:

{ 'a': true, 'b': true, 'c': true}

How can I do this?

const obj = {...arr: true} 

did not work

about 4 years ago · Santiago Gelvez
3 Respuestas
Responde la pregunta

0

Yeah, that's not a valid assignment. The first way that comes to mind - if you want one of those cool ES6 one-liners - would be:

const obj = Object.fromEntries(arr.map((el) => [el, true]));
about 4 years ago · Santiago Gelvez Denunciar

0

Using Array#reduce:

const arr = ['a', 'b', 'c'];

const res = arr.reduce((acc, key) => ({ ...acc, [key]: true }), {});

console.log(res);

about 4 years ago · Santiago Gelvez Denunciar

0

Using Object.assign and spread the array (with map).

const arr = ['a', 'b', 'c'];

const res = Object.assign({}, ...arr.map(key => ({[key]: true})));

console.log(res);

about 4 years ago · Santiago Gelvez 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