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

178
Vistas
JS create an array with objects from other array
let array1 = [1,2,3,4,5]
let array2 = [6,7,8,9,10]
let array3 = [ {x: array1, y: array2} ]

How to write correctly loop, so in output I will get 1 array with 5 ojects inside?

//Expected output

[ {x: 1, y: 6}, {x: 2, y: 7}, {x: 3, y: 8}, {x: 4, y: 9}, {x: 5, y: 10}]
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

You should loop through all the values of the first array with .map which generates a new array. The function inside of .map takes the value from array1 as value and array2 as array2[index];

let array1 = [1,2,3,4,5]
let array2 = [6,7,8,9,10]
let array3 = array1.map(function (value, index) {
    return {x: value, y: array2[index]}
})
console.log(array3);

I've intentionally used slightly more verbose solution that could be required for easier readability and understanding.

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