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

185
Vistas
How do I push to an array that is nested in an object?

I have been working on this for hours and can't figure it out. I have a checklist object sorted by category, and each category is an array of task objects. I am trying to allow the user to add a task to a given category array, but cannot figure it out. Each time I try to log the array, it just prints out the observer and says that '.push' is not a function.

I think the issue is dealing with using a variable to get the array because when I hard code in 'masterChecklist["General Cleaning"]', it will work. Please help!

** The example below is simplified

var masterChecklist = {

    "General Cleaning": 
         [
             {cleaned: false, notes: "", name: 'Empty and sanitize trash bins'},
             {cleaned: false, notes: "", name: 'Clean mirrors and windows'},
         ],

     "Home Exterior": 
         [
             {cleaned: false, notes: "", name: 'Wipe and clean all furniture'},
             {cleaned: false, notes: "", name: 'Empty the trash bins'},
         ],
}

SaveTaskDetails("Dust", "Make sure to get the shelves", "General Cleaning")

function SaveTaskDetails(name, notes, category) {
  var newTask = {
    name: name,
    notes: notes,
    cleaned: false
  }
  var category = masterChecklist[category].push(newTask)
}

console.log(masterChecklist);

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

0

You can do:

const masterChecklist = {'General Cleaning': [{ cleaned: false, notes: '', name: 'Empty and sanitize trash bins' },{ cleaned: false, notes: '', name: 'Clean mirrors and windows' },],'Home Exterior': [{ cleaned: false, notes: '', name: 'Wipe and clean all furniture' },{ cleaned: false, notes: '', name: 'Empty the trash bins' }]}

const SaveTaskDetails = (name, notes, category) => {
  if (!masterChecklist[category]) {
    masterChecklist[category] = []
  }

  masterChecklist[category].push({ name, notes, cleaned: false })
}

SaveTaskDetails('Dust', 'Make sure to get the shelves', 'General Cleaning')
SaveTaskDetails('Dust', 'Make sure to get the shelves', 'New Category')

console.log(masterChecklist)

about 4 years ago · Juan Pablo Isaza Denunciar

0

You should check if the category exists, if not you should assign an empty array and add into it.(I assumed on my code that the category does not exist because it gives an error when the category does not exist.)(By the way I highly recommend you to use const and let instead of var)

enter image description here

about 4 years ago · Juan Pablo Isaza Denunciar

0

You Should Not Use Spaces In Object Key, use _ instead

you can check your variable before doing any operation with typeof operator

example

var masterChecklist = {

    "General Cleaning": 
         [
             {cleaned: false, notes: "", name: 'Empty and sanitize trash bins'},
             {cleaned: false, notes: "", name: 'Clean mirrors and windows'},
         ],

     "Home Exterior": 
         [
             {cleaned: false, notes: "", name: 'Wipe and clean all furniture'},
             {cleaned: false, notes: "", name: 'Empty the trash bins'},
         ],
}

SaveTaskDetails("Dust", "Make sure to get the shelves", "General Cleaning")
SaveTaskDetails("Dust", "Make sure to get the shelves", "New Category")

function SaveTaskDetails(name, notes, category) {
  var newTask = {
    name: name,
    notes: notes,
    cleaned: false
  }
  var selectedCatg = typeof masterChecklist[category] == "undefined"?masterChecklist[category] = []:masterChecklist[category]
  selectedCatg.push(newTask)
}

console.log(masterChecklist);
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