Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

184
Visualizações
¿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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda