Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

182
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!