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

127
Vistas
Create object of objects from arrays

I have two arrays in JavaScript:

a = [2, 5, 8, 10, 12, 15]

and

b = ["2022-01-01", "2022-01-02", "2022-01-03", "2022-01-04", "2022-01-05", "2022-01-06"]

I want to turn this into an object of objects, like so:

ts = {
  {
    value: 2,
    time: "2022-01-01"
  },
  {
    value: 5,
    time: "2022-01-02"
  },
  {
    value: 8,
    time: "2022-01-03"
  },
  {
    value: 10,
    time: "2022-01-04"
  },
  {
    value: 12,
    time: "2022-01-05"
  },
  { 
    value: 15,
    time: "2022-01-06"
  }   
}

I have looked at the forEach method and the reduce method, e.g. from https://bobbyhadz.com/blog/javascript-create-object-from-two-arrays , but I am struggling. Edit: my attempt was along the lines of:

const ts = {};
a.forEach((a_value, index) => {
  ts.value[index] = a_value[index];
});
about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

A simple for loop will work if both lists are the same size.

let a = [2, 5, 8, 10, 12, 15]
let b = ["2022-01-01", "2022-01-02", "2022-01-03", "2022-01-04", "2022-01-05", "2022-01-06"]

let ts = []

for (let idx in a) {
  ts.push({value: a[idx],
    time: b[idx]})
}

console.log(ts)

about 4 years ago · Santiago Trujillo 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