Recursively filter array of objects Recursively filter array of objects arrays arrays

Recursively filter array of objects


Using .filter() and making a recursive call as I described in the comment above is basically what you need. You just need to update each .children property with the result of the recursive call before returning.

The return value is just the .length of the resulting .children collection, so if there's at least one, the object is kept.

var res = input.filter(function f(o) {  if (o.value.includes("Hit")) return true  if (o.children) {    return (o.children = o.children.filter(f)).length  }})