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

86
Vistas
Build nested Object/Array with jquery

I'm trying to build a javascript object from the inputs of a dynamic form. This is what I'm trying to do:

fieldTickets = [];
thisWeek = randomDateOnSunday;
weekDay = randomWeekDayWithinTheWeek;
fieldTickets[thisWeek]['days'][weekDay]['fieldTechs'].push({name: fieldTechName});

As you can see, I have some keys that are dynamic variables and some that are defined ('days' and 'fieldTechs' are always the same, but the week and weekday will change). Unfortunately every time I try to run this, even on fiddles, it gives me Cannot read properties of undefined (reading 'days') as an error. I've been dealing with this for hours and can't figure it out.

How does one build a nested array/object with dynamic keys?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

i thought that it would create it automatically if it didn't exist by using that syntax...obviously I'm wrong but is there another shorter way than a ton of if exists statements?

JavaScript will not create it automatically, but can do it without if, try something like below

const thisWeek = '12'; //randomDateOnSunday;
const weekDay = 'Sunday'; // randomWeekDayWithinTheWeek;

fieldTickets = {
  [thisWeek]: {
    days: {
      [weekDay]: {
        fieldTechs: []
      }
    }
  }
};

console.log('fieldTickets:', fieldTickets);

// pushing 
fieldTickets[thisWeek]['days'][weekDay]['fieldTechs'].push({
  name: "Hello"
});

// pushing again
fieldTickets[thisWeek]['days'][weekDay]['fieldTechs'].push({
  name: "World"
});
console.log('fieldTickets 2:', fieldTickets);

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