Skip to content

Commit 91ede64

Browse files
day 27 task complete
1 parent 2730ffd commit 91ede64

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

day27/leetcode2705.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// 2705. Compact Object
2+
// URL -> https://leetcode.com/problems/compact-object/
3+
4+
/**
5+
* @param {Object|Array} obj
6+
* @return {Object|Array}
7+
*/
8+
var compactObject = function (obj) {
9+
function dfs(obj) {
10+
// no value check
11+
if (!obj) return false
12+
13+
if (typeof obj !== 'object') return obj
14+
15+
// array check
16+
if (Array.isArray(obj)) {
17+
const newArr = [];
18+
for (let item of obj) {
19+
const subRes = dfs(item);
20+
if (subRes) {
21+
newArr.push(subRes);
22+
}
23+
}
24+
25+
return newArr;
26+
}
27+
28+
// object check
29+
const newObj = {};
30+
31+
for (const key in obj) {
32+
const subRes = dfs(obj[key])
33+
if (subRes) {
34+
newObj[key] = subRes;
35+
}
36+
}
37+
38+
return newObj;
39+
}
40+
41+
return dfs(obj);
42+
};

0 commit comments

Comments
 (0)