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);
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)
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)
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);