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

183
Vistas
¿Cómo presiono una matriz que está anidada en un objeto?

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

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

0

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)

about 4 years ago · Juan Pablo Isaza Denunciar

0

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)

ingrese la descripción de la imagen aquí

about 4 years ago · Juan Pablo Isaza Denunciar

0

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