1

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/

2
  • The thing is you have multiple levels of nesting there. How many levels should you check?
    – Piyin
    Oct 20, 2017 at 21:17
  • 1
    As of now I have face 2 levels ,it might go up to 3 at certain level if the data changes(might)
    – SNT
    Oct 20, 2017 at 21:18

1 Answer 1

2

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;
    }
}));

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.