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

184
Views
Recursion forEach no actualiza mis valores? usando Javascript

Necesito actualizar mis valores con recursividad pero mi bucle no funciona. Primer código de verificación:

 const minusItem = (items) => { console.log("items", items); if (items.length > 0) { items.forEach((orgUnit) => { orgUnit.dashCheck = true; orgUnit.allChildItemChecked = false; if (orgUnit.parentNode.unitId) { minusItem(orgUnit.parentNode); //recursion } }); } };

los elementos son una matriz, pero lo peor de todo es que a veces se envía un objeto, pero hay un si que dice

artículos.longitud > 0

aquí filtro solo cadenas que tienen longitud> 0

matriz es como

 [ { unitId: 1 , title: "Test 1 " , dashCheck: false, allChildItemChecked: false parentNode: { unitId: 2 , title: "Test 2" , dashCheck: false, allChildItemChecked: false, parentNode: { unitId: 3 , title: "Test 3" , dashCheck: false, allChildItemChecked: false, } } } ]

de estos parentNode puede tener 100....

Necesito repetir el pensamiento de cada uno y establecer

 orgUnit.dashCheck = true; orgUnit.allChildItemChecked = false;
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Solo tienes una matriz. Los objetos anidados no son matrices, por lo que no tienen una propiedad de length .

forEach debe moverse fuera de la función recursiva, donde se ejecutará solo una vez:

 const minusItem = (item) => { item.dashCheck = true; item.allChildItemChecked = false; if (item.parentNode) { // Just check if there is a parent node minusItem(item.parentNode); //recursion } }; let data = [{ unitId: 1 , title: "Test 1 " , dashCheck: false, allChildItemChecked: false, parentNode: { unitId: 2 , title: "Test 2" , dashCheck: false, allChildItemChecked: false, parentNode: { unitId: 3 , title: "Test 3" , dashCheck: false, allChildItemChecked: false, } } }]; data.forEach(minusItem); // The only place to perform a loop console.log(data);

Moviendo la iteración dentro de una función:

Si necesita que la función principal sea minusItem y se llame a la matriz original, cree una segunda función para la parte recursiva:

 const minusItem = (items) => items.forEach(minusItemRecur); const minusItemRecur = (item) => { item.dashCheck = true; item.allChildItemChecked = false; if (item.parentNode) { // Just check if there is a parent node minusItemRecur(item.parentNode); //recursion } }; let data = [{ unitId: 1 , title: "Test 1 " , dashCheck: false, allChildItemChecked: false, parentNode: { unitId: 2 , title: "Test 2" , dashCheck: false, allChildItemChecked: false, parentNode: { unitId: 3 , title: "Test 3" , dashCheck: false, allChildItemChecked: false, } } }]; minusItem(data); console.log(data);

about 4 years ago · Juan Pablo Isaza Report

0

Lodash cloneDeepWith si no te importa

 const data = [{unitId: 1 ,title:"Test 1 ",dashCheck: false,allChildItemChecked:false,parentNode:{unitId:2,title:"Test 2" ,dashCheck:false,allChildItemChecked:false,parentNode:{unitId:3 ,title:"Test 3",dashCheck:false,allChildItemChecked:false}}}]; const result = _.cloneDeepWith(data, (val, key) => { if (key === 'dashCheck') return true; if (key === 'allChildItemChecked') return false; }); console.log(result);
 .as-console-wrapper{min-height: 100%!important; top: 0}
 <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>

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!