Filter Elements Across All Levels of a Multidimensional Nested Array

Extend the concept of Array.filter() to work on multi-dimensional (nested) arrays. Write a function that applies a predicate to every element at every depth level and returns a new nested array of the same structure containing only matching elements. Empty sub-arrays resulting from filtering should be either preserved or pruned based on a flag. This combines recursive tree traversal with functional filter semantics.

Examples
Input: []
Output:
Hints

Filter Elements Across All Levels of a Multidimensional Nested Array

Extend the concept of Array.filter() to work on multi-dimensional (nested) arrays. Write a function that applies a predicate to every element at every depth level and returns a new nested array of the same structure containing only matching elements. Empty sub-arrays resulting from filtering should be either preserved or pruned based on a flag. This combines recursive tree traversal with functional filter semantics.