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

182
Vistas
Recursion forEach does not update my values? using Javascript

I need to update my values with recursion but my loop no work? First check code:

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

items is array but worst of all sometimes an object is sent but there is an if that says

items.length > 0

here I filter only strings that have length> 0

array is like

[
 { 
 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, 
        }
      } 
   }
]

of these parentNode can have 100....

I need to loop thought each and set

    orgUnit.dashCheck = true;
    orgUnit.allChildItemChecked = false;
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You only have one array. The nested objects are not arrays, so they don't have a length property.

The forEach should be moved outside of the recursive function, where it will execute only once:

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

Moving the iteration inside a function:

If you need the main function to be minusItem and to be called on the original array, then make a second function for the recursive part:

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 Denunciar

0

Lodash cloneDeepWith if you don't mind

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