Deep Object Filter Problem
Description
LeetCode Problem 2823.
Given an object or an array obj and a function fn, return a filtered object or array filteredObject.
Function deepFilter should perform a deep filter operation on the obj. The deep filter operation should remove properties for which the output of the filter function fn is false, as well as any empty objects or arrays that remain after the keys have been removed.
If the deep filter operation results in an empty object or array, with no remaining properties, deepFilter should return undefined to indicate that there is no valid data left in the filteredObject.
Example 1:
Input:
obj = [-5, -4, -3, -2, -1, 0, 1],
fn = (x) => x > 0
Output: [1]
Explanation: All values that were not greater than 0 were removed.
Example 2:
Input:
obj = {"a": 1, "b": "2", "c": 3, "d": "4", "e": 5, "f": 6, "g": {"a": 1}},
fn = (x) => typeof x === "string"
Output: {"b":"2","d":"4"}
Explanation: All keys with values that were not a string were removed. When the object keys were removed during the filtering process, any resulting empty objects were also removed.
Example 3:
Input:
obj = [-1, [-1, -1, 5, -1, 10], -1, [-1], [-5]],
fn = (x) => x > 0
Output: [[5,10]]
Explanation: All values that were not greater than 0 were removed. When the values were removed during the filtering process, any resulting empty arrays were also removed.
Example 4:
Input:
obj = [[[[5]]]],
fn = (x) => Array.isArray(x)
Output: undefined
Constraints:
fnis a function that returns a boolean valueobjis a valid JSON object or array2 <= JSON.stringify(obj).length <= 10^5
Solution
Approach: Recursive filter that returns undefined for empty results
Define a recursive helper filter(x) that returns the filtered version of x, or undefined meaning "nothing left".
- Array: filter each element recursively, drop the
undefinedresults, and return the array only if it is non-empty. - Object: do the same per property, building a new object from the surviving values; return it only if it has a key.
- Anything else is a leaf: keep it when
fn(leaf)is truthy, otherwiseundefined.
Because empty containers turn into undefined and are dropped by their parent, emptiness propagates upward on its own (Example 4: the deepest array has no match, then each outer array becomes empty in turn, so the answer is undefined). Note that fn is applied to leaves only, and null counts as a leaf, not as an object.
Complexity: Time O(n) for n values in the structure, space O(d) recursion depth plus the output.
/**
* @param {Object|Array} obj
* @param {Function} fn
* @return {Object|Array|undefined}
*/
var deepFilter = function (obj, fn) {
const filter = x => {
if (Array.isArray(x)) {
const kept = x.map(filter).filter(v => v !== undefined);
return kept.length > 0 ? kept : undefined;
}
if (typeof x === 'object' && x !== null) {
const kept = {};
for (const [key, value] of Object.entries(x)) {
const v = filter(value);
if (v !== undefined) {
Object.defineProperty(kept, key, {
value: v,
writable: true,
enumerable: true,
configurable: true,
});
}
}
return Object.keys(kept).length > 0 ? kept : undefined;
}
return fn(x) ? x : undefined;
};
return filter(obj);
};