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

132
Vistas
How to map out two arrays into array of object

I have two arrays

keys:[key0, key1, key2, key3]

and

body:[['jake', 24, 3, true], ['susan', 21, 0, true]]

the result I need is

[{key0:jake, key1:24, key2:3, key3:true}, {key0:susan, key1:21, key2:0, key3:true}]
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

Map the keys and values to an arrays of pairs of [key, value], and then convert to an object using Object.fromEntries() (see zipObject function).

To convert an array of values, use Array.map() with zipObject:

const zipObject = keys => values => Object.fromEntries(keys.map((key, i) => [key, values[i]]))

const keys = ['key0', 'key1', 'key2', 'key3']

const values = [['jake', 24, 3, true], ['susan', 21, 0, true]]

const result = values.map(zipObject(keys))

console.log(result)

about 4 years ago · Juan Pablo Isaza Denunciar

0

const body = [['jake', 24, 3, true], ['susan', 21, 0, true]];
const keys = ['key0', 'key1', 'key2', 'key3']
let newArray = [];

numbers.forEach((value) =>
{
    let obj={};
    keys.forEach((key,index) => 
{
        obj[`${key}`] = value[index];
        
    })
    newArray.push(obj);
    
});
console.log(newArray)
about 4 years ago · Juan Pablo Isaza Denunciar

0

This may be one simple way to do this, using easy to read loops:

const keys = ['key0', 'key1', 'key2', 'key3'];
const body = [['jake', 24, 3, true], ['susan', 21, 0, true]];
const result = [];
for (let b of body) {
    const obj = {};
    for (let i = 0; i < b.length; i++) {
        obj[keys[i]] = b[i];
    }
    result.push(obj);
}
console.log(result);

Result:

[
  { key0: 'jake', key1: 24, key2: 3, key3: true },
  { key0: 'susan', key1: 21, key2: 0, key3: true }
]
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