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

168
Vistas
Nth order tree make all parents false if any one of last child is true

1.I have a use case where i want the parent to be made false if any last child "isSelected" is true. For example the id 4 isSelcted is true so the parents of 4 are 3,2,1 whose key clicked should be made false.

2.Example 2: If Id 7 isSelected is true then its parent 6,2,1 whose key clicked should be made false.

3.I have a tree in angular , on edit page I'll have a data in the above format. I have to expand the tree if any one of the child is selected. For example id 4 is the last child which contains isSelected falg and is true. Hence I have to make all the parent clicked false so that using ngbBootstrap [collapse] property i'll expand the tree.

4.reduce is used to check if any children has a value or clicked if so add it to the accumulator , !1 is used to initialize the accumulator to 0. if the reduce returns with value more than one then assign clicked to ! of the value that is false. To check the end of the tree. ie the last child isSelcted is used for ternary operator.

5.This code is not working as am missing something, please correct where am going wrong.


let update = o => o.children ? !o.clicked = o.children.reduce((a, c) => a || o.clicked || update(c), !1) : o.isSelected;

update(object);

console.log(object);

let object = {
    children: [{
        ID: 1,
        clicked: false,
        children: [{
                ID: 2,
                clicked: ture,
                children: [{
                        ID: 3,
                        clicked: true,
                        children: [{
                                ID: 4,
                                isSelected: true
                            },
                            {
                                ID: 5,
                                isSelected: true
                            }
                        ]
                    },
                    {
                        ID: 6,
                        clicked: false,
                        children: [{
                                ID: 7,
                                isSelected: false
                            },
                            {
                                ID: 8,
                                isSelected: false
                            }
                        ]
                    }
                ]
            },
            {
                ID: 9,
                clicked: true,
                children: [{
                        ID: 10,
                        isSelected: false
                    },
                    {
                        ID: 11,
                        isSelected: true
                    }
                ]
            }
        ]
    }]
}
about 4 years ago · Juan Pablo Isaza
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