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

157
Views
atravesar un objeto y agregar una nueva propiedad/objeto?

considere este JSON como un producto y podría tener infinitos productos secundarios.

Necesito una función que recorra la propiedad de productos secundarios y agregue un elemento secundario en el lugar correcto:

 export const addChildProduct = (childTree, nestedChildIndex, updatedChild) => { let newChild = childTree; for (let i = 0; i < nestedChildIndex.length; i++) { newChild.childProducts[nestedChildIndex[i]].listOfNewCustomProduct[nestedChildIndex[i]]; } newChild.childProducts === null ? (newChild.childProducts = Array.of(updatedChild)) : newChild.childProducts.push(updatedChild); return newChild; };

para un mejor entendimiento:

childTree es la matriz principal/primer childProduct del producto.

nestedChildIndex es la matriz de índices que determina qué tan profundo debo profundizar dentro de childProducts , es algo como esto: ['0', '0'] dice que en los productos secundarios debo ir al index 0 de eso y luego dentro de childProducts of index 0 , debería ir al índice 0 (espero que no te hayas confundido)

y updatedChild es el nuevo objeto secundario que necesito agregar. (Lo obtengo de un formulario pero el formato es el mismo que otros)

Entonces, ¿cómo puedo alterar este bucle para lograr mi objetivo?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Manejo el problema alterando el código así:

 export const addChildProduct = (childTree, nestedChildIndex, updatedChild) => { let newChild = childTree; for (let i = 0; i < nestedChildIndex.length; i++) { newChild = newChild.childProducts[nestedChildIndex[i]].listOfNewCustomProduct[nestedChildIndex[i]]; } newChild.childProducts === null ? (newChild.childProducts = Array.of(updatedChild)) : newChild.childProducts.push(updatedChild); return {...childTree, childProducts: newChild}; };
about 4 years ago · Juan Pablo Isaza Report

0

Te has perdido la reasignación del newChild con bucles internos

 for (let i = 0; i < nestedChildIndex.length; i++) { newChild = newChild.childProducts[nestedChildIndex[i]].listOfNewCustomProduct[nestedChildIndex[i]]; }
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!