Reputation: 1433
I am trying to filter a few items out of an array. So far I have been able to filter an item in the array, but if I want to remove a child from the array element I am unable to do so.
In the example below chartsArray
has code value Age_Total
. In array el
I have this string Age_Total
and when I filter it is removed. But I also have a field Q0120F_18P
which doesn't get filtered. You can still see this element in the fourth array element under Age_Female
. So my question is how can I iterate through chartsArray
where it can filter the parent array element as well as the child element.
https://jsfiddle.net/snt/n16pfgbk/
Upvotes: 1
Views: 54
Reputation: 386848
You could filter the children with a recursive call and assign it inside of the filter callback.
chartsArray = chartsArray.map(a => a.filter(function code(o) {
if (!el.includes(o.code)) {
if (o.children) {
o.children = o.children.filter(code);
}
return true;
}
}));
Upvotes: 2