Skip to content

Commit 5e67f9f

Browse files
authored
feat: update solutions to lc problem: No.2705 (doocs#2851)
No.2705.Compact Object
1 parent dfb0297 commit 5e67f9f

File tree

4 files changed

+78
-73
lines changed

4 files changed

+78
-73
lines changed

solution/2700-2799/2705.Compact Object/README.md

+23-24
Original file line numberDiff line numberDiff line change
@@ -78,44 +78,43 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/2700-2799/2705.Co
7878
type Obj = Record<any, any>;
7979

8080
function compactObject(obj: Obj): Obj {
81-
if (Array.isArray(obj)) {
82-
const temp = [];
83-
for (const item of obj) {
84-
if (item) {
85-
if (typeof item === 'object') temp.push(compactObject(item));
86-
else temp.push(item);
87-
}
88-
}
89-
return temp;
81+
if (!obj || typeof obj !== 'object') {
82+
return obj;
9083
}
91-
for (const [key, value] of Object.entries(obj)) {
92-
if (!value) delete obj[key];
93-
else if (typeof value === 'object') obj[key] = compactObject(value);
84+
if (Array.isArray(obj)) {
85+
return obj.map(compactObject).filter(Boolean);
9486
}
95-
return obj;
87+
return Object.entries(obj).reduce((acc, [key, value]) => {
88+
const compactedValue = compactObject(value);
89+
if (compactedValue) {
90+
acc[key] = compactedValue;
91+
}
92+
return acc;
93+
}, {} as Obj);
9694
}
9795
```
9896

9997
#### JavaScript
10098

10199
```js
100+
/**
101+
* @param {Object|Array} obj
102+
* @return {Object|Array}
103+
*/
102104
var compactObject = function (obj) {
103-
if (obj === null || typeof obj !== 'object') {
105+
if (!obj || typeof obj !== 'object') {
104106
return obj;
105107
}
106-
107108
if (Array.isArray(obj)) {
108-
return obj.filter(Boolean).map(compactObject);
109+
return obj.map(compactObject).filter(Boolean);
109110
}
110-
111-
const result = {};
112-
for (const key in obj) {
113-
const value = compactObject(obj[key]);
114-
if (Boolean(value)) {
115-
result[key] = value;
111+
return Object.entries(obj).reduce((acc, [key, value]) => {
112+
const compactedValue = compactObject(value);
113+
if (compactedValue) {
114+
acc[key] = compactedValue;
116115
}
117-
}
118-
return result;
116+
return acc;
117+
}, {});
119118
};
120119
```
121120

solution/2700-2799/2705.Compact Object/README_EN.md

+32-25
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,15 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/2700-2799/2705.Co
5858

5959
<!-- solution:start -->
6060

61-
### Solution 1
61+
### Solution 1: Recursion
62+
63+
If `obj` is not an object or is null, the function will return it as is, because there's no need to check for keys in non-object values.
64+
65+
If `obj` is an array, it will use `obj.filter(Boolean)` to filter out falsy values (like `null`, `undefined`, `false`, 0, ""), then use `map(compactObject)` to recursively call `compactObject` on each element. This ensures that nested arrays are also compacted.
66+
67+
If `obj` is an object, it will create a new empty object `compactedObj`. It will iterate over all keys of `obj`, and for each key, it will recursively call `compactObject` on the corresponding value, then store the result in the value variable. If the value is truthy (i.e., not falsy), it will assign it to the compacted object with the corresponding key.
68+
69+
The time complexity is $O(n)$, and the space complexity is $O(n)$.
6270

6371
<!-- tabs:start -->
6472

@@ -68,44 +76,43 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/2700-2799/2705.Co
6876
type Obj = Record<any, any>;
6977

7078
function compactObject(obj: Obj): Obj {
71-
if (Array.isArray(obj)) {
72-
const temp = [];
73-
for (const item of obj) {
74-
if (item) {
75-
if (typeof item === 'object') temp.push(compactObject(item));
76-
else temp.push(item);
77-
}
78-
}
79-
return temp;
79+
if (!obj || typeof obj !== 'object') {
80+
return obj;
8081
}
81-
for (const [key, value] of Object.entries(obj)) {
82-
if (!value) delete obj[key];
83-
else if (typeof value === 'object') obj[key] = compactObject(value);
82+
if (Array.isArray(obj)) {
83+
return obj.map(compactObject).filter(Boolean);
8484
}
85-
return obj;
85+
return Object.entries(obj).reduce((acc, [key, value]) => {
86+
const compactedValue = compactObject(value);
87+
if (compactedValue) {
88+
acc[key] = compactedValue;
89+
}
90+
return acc;
91+
}, {} as Obj);
8692
}
8793
```
8894

8995
#### JavaScript
9096

9197
```js
98+
/**
99+
* @param {Object|Array} obj
100+
* @return {Object|Array}
101+
*/
92102
var compactObject = function (obj) {
93-
if (obj === null || typeof obj !== 'object') {
103+
if (!obj || typeof obj !== 'object') {
94104
return obj;
95105
}
96-
97106
if (Array.isArray(obj)) {
98-
return obj.filter(Boolean).map(compactObject);
107+
return obj.map(compactObject).filter(Boolean);
99108
}
100-
101-
const result = {};
102-
for (const key in obj) {
103-
const value = compactObject(obj[key]);
104-
if (Boolean(value)) {
105-
result[key] = value;
109+
return Object.entries(obj).reduce((acc, [key, value]) => {
110+
const compactedValue = compactObject(value);
111+
if (compactedValue) {
112+
acc[key] = compactedValue;
106113
}
107-
}
108-
return result;
114+
return acc;
115+
}, {});
109116
};
110117
```
111118

Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1+
/**
2+
* @param {Object|Array} obj
3+
* @return {Object|Array}
4+
*/
15
var compactObject = function (obj) {
2-
if (obj === null || typeof obj !== 'object') {
6+
if (!obj || typeof obj !== 'object') {
37
return obj;
48
}
5-
69
if (Array.isArray(obj)) {
7-
return obj.filter(Boolean).map(compactObject);
10+
return obj.map(compactObject).filter(Boolean);
811
}
9-
10-
const result = {};
11-
for (const key in obj) {
12-
const value = compactObject(obj[key]);
13-
if (Boolean(value)) {
14-
result[key] = value;
12+
return Object.entries(obj).reduce((acc, [key, value]) => {
13+
const compactedValue = compactObject(value);
14+
if (compactedValue) {
15+
acc[key] = compactedValue;
1516
}
16-
}
17-
return result;
17+
return acc;
18+
}, {});
1819
};
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
type Obj = Record<any, any>;
22

33
function compactObject(obj: Obj): Obj {
4-
if (Array.isArray(obj)) {
5-
const temp = [];
6-
for (const item of obj) {
7-
if (item) {
8-
if (typeof item === 'object') temp.push(compactObject(item));
9-
else temp.push(item);
10-
}
11-
}
12-
return temp;
4+
if (!obj || typeof obj !== 'object') {
5+
return obj;
136
}
14-
for (const [key, value] of Object.entries(obj)) {
15-
if (!value) delete obj[key];
16-
else if (typeof value === 'object') obj[key] = compactObject(value);
7+
if (Array.isArray(obj)) {
8+
return obj.map(compactObject).filter(Boolean);
179
}
18-
return obj;
10+
return Object.entries(obj).reduce((acc, [key, value]) => {
11+
const compactedValue = compactObject(value);
12+
if (compactedValue) {
13+
acc[key] = compactedValue;
14+
}
15+
return acc;
16+
}, {} as Obj);
1917
}

0 commit comments

Comments
 (0)