-
Notifications
You must be signed in to change notification settings - Fork 0
/
6_array_chunk.js
59 lines (51 loc) · 1.57 KB
/
6_array_chunk.js
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
51
52
53
54
55
56
57
58
59
/**
* 6. Array Chunk (数组块问题)
* 描述: Given an array and chunk size, divide the array into many subarrays
* where each subarray is of length size
*
* Examples:
* chunk([1, 2, 3, 4], 2) --> [[ 1, 2], [3, 4]]
* chunk([1, 2, 3, 4, 5], 2) --> [[ 1, 2], [3, 4], [5]]
* chunk([1, 2, 3, 4, 5, 6, 7, 8], 3) --> [[ 1, 2, 3], [4, 5, 6], [7, 8]]
* chunk([1, 2, 3, 4, 5], 4) --> [[ 1, 2, 3, 4], [5]]
* chunk([1, 2, 3, 4, 5], 10) --> [[ 1, 2, 3, 4, 5]]
*
* 语言: Javascript(ES6)
*/
// 1. method one
function chunk1(array, size){
// 1. declare a new array
const chunked = [];
for(let element of array){
const last = chunked[chunked.length - 1];
// console.log("last", last)
if(!last || last.length === size){
// console.log("element", [element]);
chunked.push([element]);
} else {
last.push(element);
}
}
return chunked;
}
// 2. method two
// Main point: slice()
function chunk2(array, size){
const chunked = [];
let index = 0;
while (index < array.length){
chunked.push(array.slice(index, index + size));
index += size;
}
return chunked;
}
// check method one
console.log(chunk1([1, 2, 3, 4], 2));
console.log(chunk1([1, 2, 3, 4, 5], 4));
console.log(chunk1([1, 2, 3, 4, 5], 10));
console.log(chunk1([1, 2, 3, 4, 5, 6, 7, 8], 3));
// check method two
console.log(chunk2([1, 2, 3, 4], 2));
console.log(chunk2([1, 2, 3, 4, 5], 4));
console.log(chunk2([1, 2, 3, 4, 5], 10));
console.log(chunk2([1, 2, 3, 4, 5, 6, 7, 8], 3));