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
Atraviesa una matriz recursiva y actualiza

Estoy tratando de atravesar una matriz y actualizar el elemento cuando lo encuentro. He buscado en Google y SO, pero supongo que no estoy usando las palabras clave adecuadas para hacerlo.

Problema

Tengo el siguiente objeto:

 const form = { uuid: "ff17fd11-3462-4111-b083-7ff36b391c61", type: "form", items: [ { uuid: "0b2dcf80-a376-4b3d-9491-0875ec24d6f7", type: "page", items: [ { uuid: "23a8db85-4125-43db-ae04-d1243738972c", title: "Prepared by", }, { uuid: "fb3898e0-c250-4dde-8b5c-cd2c99d181e9", title: "Completed at", } ], params: { header: true, collapsed: true } }, { title: "", uuid: "e0d9d8c4-c064-4088-aad3-235b6ea1a6a1", type: "page", items: [ { uuid: "73f97b99-9788-4748-82c6-7b62169f44fe", title: "you have been rickrolled", }, { uuid: "46e7264c-7e95-4bbf-a4e7-7b7ab2d1ad88", type: "conditional_section", items: [ { uuid: "58e5e845-d18e-4c7e-9739-a387b8144d3c", params: { title: null, }, } ], ], } ] };

Dado el siguiente objeto, me gustaría encontrar este objeto en la matriz (identificado por uuid ) y reemplazarlo con el nuevo:

 👇 Heres the uuid that identifies the unique element in the array const updatedObject = { uuid: "58e5e845-d18e-4c7e-9739-a387b8144d3c", params: { title: "new title" } }; findFormItem(form.items, updatedObject);

Lo que he probado hasta ahora

Creé una función de recursión para actualizar la matriz, pero por alguna razón, el elemento no se actualiza:

 const findFormItem = (arr, item, parent = null) => arr.reduce((acc, _item) => { if (acc) { return acc; } if (_item.uuid === item.uuid) { _item = item; } if (_item.items) { return findFormItem(_item.items, item, _item); } return acc; }, null);

Pregunta

¿Cómo puedo actualizar la matriz/objeto sin tener que devolver una nueva copia? Estoy buscando una solución que no altere la forma del objeto. El objeto debe permanecer como está, solo actualizando el valor que se pasa a la función findFormItem

codigosandbox

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

0

Si lo que desea es cambiar la matriz original, puede usar forEach y reemplazar el objeto en un index dado:

 const findFormItem = (arr, item) => { arr.forEach((element, index, arr) => { if (element.uuid === item.uuid) { arr[index] = item; } if (element.items) { findFormItem(element.items, item); } }); };
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!