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