Skip to content

Commit 8c3768c

Browse files
committed
Section 05 : JSON - Part 01
1 parent a8e26ce commit 8c3768c

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

5. JSON/20 2727. Is Object Empty.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
var isEmpty = function (obj) {
2+
if (Array.isArray(obj)) {
3+
// Check if the array has zero elements
4+
return obj.length === 0
5+
} else if (typeof obj === 'object' && obj !== null) {
6+
// Check if the object has zero key-value pairs
7+
return Object.keys(obj).length === 0
8+
} else {
9+
// If obj is neither an array nor an object, it's considered empty
10+
return true
11+
}
12+
}

5. JSON/21 2677. Chunk Array.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
var chunk = function (arr, size) {
2+
const chunkedArray = []
3+
let index = 0
4+
5+
while (index < arr.length) {
6+
let count = size
7+
const temp = []
8+
9+
while (count-- > 0 && index < arr.length) {
10+
temp.push(arr[index])
11+
index++
12+
}
13+
14+
chunkedArray.push(temp)
15+
}
16+
17+
return chunkedArray
18+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Array.prototype.last = function () {
2+
if (this.length) {
3+
return this[this.length - 1]
4+
}
5+
return -1
6+
}

5. JSON/23 2631. Group By.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Array.prototype.groupBy = function (fn) {
2+
const result = {}
3+
for (const obj of this) {
4+
const key = fn(obj)
5+
result[key] = result[key] || []
6+
result[key].push(obj)
7+
}
8+
return result
9+
}

0 commit comments

Comments
 (0)