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

253
Views
¿Cómo puedo distribuir elementos de matriz en un objeto como claves y definir algún valor para ellos?

Digamos que tengo una matriz:

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

Quiero crear un objeto como este:

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

¿Cómo puedo hacer esto?

 const obj = {...arr: true}

no funcionó

about 4 years ago · Santiago Gelvez
3 answers
Answer question

0

Sí, esa no es una tarea válida. La primera forma que se te ocurre, si quieres uno de esos ingeniosos ES6 de una sola línea, sería:

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

0

Usando 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 Report

0

Usando Object.assign y distribuya la matriz (con el mapa).

 const arr = ['a', 'b', 'c']; const res = Object.assign({}, ...arr.map(key => ({[key]: true}))); console.log(res);

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