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

77
Vistas
Advanced in converting arrays to objects

I'm struggling with the following javascript problem. Is there any way to do it like this?

// input, based on this data, I want the output to be like below
const ListValues = ["id", "name", "description"]

// output
const initialValues = {
    id: "",
    name: "",
    description: "",
}; 

Thank you so much and Have a good day <3

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You can use Object.fromEntries() and Array.map() to create your desired object from the input.

const ListValues = ["id", "name", "description"];

const initialValues = Object.fromEntries(ListValues.map(v => [v, '']));

console.log('initialValues:', initialValues)
.as-console-wrapper { max-height: 100% !important; top: 0; }

You could also use Array.reduce() to get the same result:

const ListValues = ["id", "name", "description"];

const initialValues = ListValues.reduce((acc, cur) => { 
    acc[cur] = '';
    return acc;
}, {})

console.log('initialValues:', initialValues)
.as-console-wrapper { max-height: 100% !important; top: 0; }

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