Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

167
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda