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

157
Vistas
Converting an array of arrays into an object with key and value with javascript

method input: (['name', 'marcus'], ['address', 'New York']

method output: {name: marcus, address: New York}

How do i do it?

cont array = [['c','2'],['d','4']];

function objectify()
{
    array.forEach(arrayElement => { for(let i = 0; i<2; i++){
            const obj = Object.assign({array[i][i]}, array[i++])
    }
    return obj;
})

}

console.log(objectify);
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You can use Object.fromEntries()

const data = [['name', 'marcus'], ['address', 'New York']];
const result = Object.fromEntries(data);
console.log(result);

about 4 years ago · Juan Pablo Isaza Denunciar

0

1) If you are using forEach then you don't have to use for loop inside it

array.forEach(arrayElement => { for(let i = 0; i<2; i++){
            const obj = Object.assign({array[i][i]}, array[i++])
    }

2) You shouldn't hardcode the length upto which i will run, because it can be of any length.

for(let i = 0; i<2; i++)

If you are using forEach then you should loop over the array and get the key and value and set it into the resultant object.

function objectify(array) {
  const result = {};
  
  array.forEach((o) => {
    const [key, value] = o;
    result[key] = value;
  });

  return result;
}

const array = [
  ["c", "2"],
  ["d", "4"],
];
console.log(objectify(array));

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