He estado trabajando en esto durante horas y no puedo resolverlo. Tengo un objeto de lista de verificación ordenado por categoría, y cada categoría es una matriz de objetos de tarea. Estoy tratando de permitir que el usuario agregue una tarea a una matriz de categoría dada, pero no puedo resolverlo. Cada vez que intento registrar la matriz, simplemente imprime el observador y dice que '.push' no es una función.
Creo que el problema es lidiar con el uso de una variable para obtener la matriz porque cuando codifico en 'masterChecklist ["Limpieza general"]', funcionará. ¡Por favor ayuda!
** El siguiente ejemplo está simplificado
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);Tu puedes hacer:
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)Debe verificar si la categoría existe, si no, debe asignar una matriz vacía y agregarla (supuse en mi código que la categoría no existe porque da un error cuando la categoría no existe). (Por cierto Le recomiendo que use const y let en lugar de var)
No debe usar espacios en la clave de objeto, use _ en su lugar
puede verificar su variable antes de realizar cualquier operación con el operador typeof
ejemplo
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);