-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayChunking.js
More file actions
50 lines (39 loc) · 1.38 KB
/
ArrayChunking.js
File metadata and controls
50 lines (39 loc) · 1.38 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
49
50
// CHALLENGE: ARRAY CHUNKING
// Split an array into chunked arrays of a specific length
// ex. chunkArray([1, 2, 3, 4, 5, 6, 7], 3) === [[1, 2, 3],[4, 5, 6],[7]]
// ex. chunkArray([1, 2, 3, 4, 5, 6, 7], 2) === [[1, 2],[3, 4],[5, 6],[7]]
// SOLUTION: using slice() function
// slice(start, end) slices the array
const chunkArray = (arr, len) => {
const chunkedArr = [];
// starting from (0, len) -> indexes increase by len for chunked parts.
// we can increase i by len.
for (let i = 0; i < arr.length; i = i + len) {
chunkedArr.push(arr.slice(i, i + len));
}
return chunkedArr;
};
console.log(chunkArray([1, 2, 3, 4, 5, 6, 7], 3));
console.log(chunkArray([1, 2, 3, 4, 5, 6, 7], 2));
// SOLUTION 2
const chunkArray2 = (arr, len) => {
const chunkedArr = [];
arr.forEach((val) => {
// accessing the last element
// to check if the chunked len is equal to len.
// to modify the orinal array through last one
const last = chunkedArr[chunkedArr.length - 1];
// if the last len is reached, push new array with val.
if (!last || last.length === len) {
chunkedArr.push([val]);
}
// if not reached yet, add to the current array.
else {
last.push(val);
}
});
return chunkedArr;
};
console.log("**************************************");
console.log(chunkArray2([1, 2, 3, 4, 5, 6, 7], 3));
console.log(chunkArray2([1, 2, 3, 4, 5, 6, 7], 2));