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

137
Vistas
Js how to set an item at a nested index in an empty array

I have this array that has to be filled with objects. Each object has a slug with 4 sections divided by "-" (for example: test-dx-ef-1) and depending on it I have to place them in an array at a specific index named with the slug section.

I tried looping all the inputs and directly setting the object like so:

var data = []
objects.forEach((obj) => {

    var slug = obj.slug.split('-')
    data[slug[0]][slug[1]][slug[2]][slug[3]] = obj

}

But it didn't work, as apparently, even if data[slug[0]] can be created, data[slug[0]][slug[1]] results undefined and an error is thrown.

Is there a way in which I can accomplish this?

about 4 years ago · Juan Pablo Isaza
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