-1

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 "collapse" should be made false.

2.Example 2: If Id 7 isSelected is true then its parent 6,2,1 whose key "collapse" 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 "collapse" false so that using ngbBootstrap [collapse] property i'll expand the tree.

4.The for loop is not working, the first child of every parent is executed but the loop for children is not working, may I know what am I missing?

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

let object = {
    children: [{
        ID: 1,
        collapse: false,
        children: [{
            ID: 2,
            collapse: true,
            children: [{
                ID: 3,
                collapse: true,
                children: [{
                    ID: 4,
                    isSelected: true
                }, {
                    ID: 5,
                    isSelected: true
                }]
            }, {
                ID: 6,
                collapse: false,
                children: [{
                    ID: 7,
                    isSelected: false
                }, {
                    ID: 8,
                    isSelected: false
                }]
            }]
        }, {
            ID: 9,
            collapse: true,
            children: [{
                ID: 10,
                isSelected: false
            }, {
                ID: 11,
                isSelected: true
            }]
        }]
    }]
};

function recursiveUpdate(object) {

    function rec(obj){
        if(!obj.children){
            if(obj.isSelected){return false}else{return true}
        }else{
            for(let i=0;i<obj.children.length;i++){
                let res = rec(obj.children[i]);
                obj.collapse = res;
                return res;
            }
        }
    }
    rec(object);
}

recursiveUpdate(object);

console.log(object);


Srujan R
  • 29
  • 5
  • _"The for loop is not working"_ - It is. _"...what am I missing?"_ - What the [`return` statement](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/return) does – Andreas Apr 04 '22 at 07:56
  • Thank you @Andreas ,Okay then what alternatives do i have to loop children? – Srujan R Apr 04 '22 at 07:58

1 Answers1

-1

I figured out the solution thanks.

function recursiveUpdate(object) {
    
    function rec(obj){
        if(!obj.children){
            if(obj.isSelected){return false}else{return true}
        }else{
            let count = 0;
            for(let i=0;i<obj.children.length;i++){
                console.log(obj.children[i],obj.children[i].ID)
                let res = rec(obj.children[i]);
                if(!res){
                    count++;
                }
            }
            let op = (count)?false:true;
            obj.collapse = op;
            return op;
        }
    }
    rec(object);
}
Srujan R
  • 29
  • 5