We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent 2730ffd commit 91ede64Copy full SHA for 91ede64
day27/leetcode2705.js
@@ -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
34
+ newObj[key] = subRes;
35
36
37
38
+ return newObj;
39
40
41
+ return dfs(obj);
42
+};
0 commit comments