-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy path2823-deep-object-filter.js
More file actions
48 lines (42 loc) · 1.47 KB
/
2823-deep-object-filter.js
File metadata and controls
48 lines (42 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/**
* 2823. Deep Object Filter
* https://leetcode.com/problems/deep-object-filter/
* Difficulty: Medium
*
* 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.
*/
/**
* @param {Object|Array} obj
* @param {Function} fn
* @return {Object|Array|undefined}
*/
var deepFilter = function(obj, fn) {
if (Array.isArray(obj)) {
const filteredArray = obj
.map(item => deepFilter(item, fn))
.filter(item => item !== undefined);
return filteredArray.length > 0 ? filteredArray : undefined;
}
if (typeof obj === 'object' && obj !== null) {
const filteredObject = {};
let hasValidProperties = false;
Object.keys(obj).forEach(key => {
const filteredValue = deepFilter(obj[key], fn);
if (filteredValue !== undefined) {
filteredObject[key] = filteredValue;
hasValidProperties = true;
}
});
return hasValidProperties ? filteredObject : undefined;
}
return fn(obj) ? obj : undefined;
};