In my current use case, I am aiming to update the array in both the if
and else
scenarios.
The primary array, referred to as cis, appears as follows:
https://i.sstatic.net/esbb8.png
.html
<sample-container [cis]="filterCi(currentBaseline.cis, currentBaseline)"> </sample-container>
.ts
This particular function involves iterating over the provided array and returning a structure identical to the original, except for objects with the property checked:false
being removed from the structure. This is achieved by recursively calling on children each time.
//retrieveChildren
retreiveChildren(children: Structure[]) : Structure[] {
let filteredCis = children && children.filter((ci) =>{
if(ci.children && ci.children.length > 0){
retreiveChildren(ci.children);
}
return ci.checked === true;
}) ;
return filteredCis;
}
//filterCi function
filterCi(cis: Structure[], baseline: Baseline): Structure[] {
if(baseline.isHideElementsActivated){
let newArray = [...cis]
let result = newArray.filter(ci => ci.checked === true).map((item,index) => {
return {
...item,
children : retreiveChildren(item.children)
}
})
console.log("Result array...." , result)
return result;
}else{
return cis;
}
}
The challenge I am encountering here is that the filterCi
function seems to be stuck in an infinite loop. Initially, I suspected it was due to the recursive call, but even after removing recursion, the issue persists.
Upon closer examination, I realized that the problem lies within the map
function used inside filterCi.
What causes this behavior, and what steps can I take to resolve it?