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

437
Vistas
Javascript: How to convert array ["a=1", "b=2"] into an object?

What is the best way to convert:

user = ['Name:Marry', 'Age:24', 'Gender:Female']

to

user = { 'Name':'Marry', 'Age':'24', 'Gender':'Female' }
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

One line solution:

const result = Object.assign({}, ...user.map(item => item.split(':')).map(item => ({ [item[0]]: item[1]})))
about 4 years ago · Juan Pablo Isaza Denunciar

0

    user = ['Name:Marry', 'Age:24', 'Gender:Female']
    const userObj = {}

    for(item of user) {
      const [key, value] = item.split(":")
      userObj[key] = value
    }
    
    console.log(userObj)

about 4 years ago · Juan Pablo Isaza Denunciar

0

Here you want to convert an array to a single value( here it is an object ), for this more declarative way is to use reduce method. Here I have given an empty object as the initial value of the accumulator and on each iteration it adds the key value pair.

const result = user.reduce((acc, curr) => {
  const [key, value] = curr.split(':');
  acc[key] = value;
  
  return acc;
}, {});

Hope this helps.

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