SHOW:
|
|
- or go back to the newest paste.
1 | - | const arr = ['a','b','c','d','e','f','g']; |
1 | + | const arr = ['a','b','c','d','e','f','g']; |
2 | - | |
2 | + | |
3 | - | |
3 | + | |
4 | - | const chunkArray = (array, length) => { |
4 | + | const chunkArray = (array, length) => { |
5 | - | let newArr = []; |
5 | + | let newArr = []; |
6 | - | let temp = []; |
6 | + | let temp = []; |
7 | - | |
7 | + | |
8 | - | for(let i = 0; i < array.length; i++){ |
8 | + | for(let i = 0; i < array.length; i++){ |
9 | - | temp.push(array[i]); |
9 | + | temp.push(array[i]); |
10 | - | if((i+1)%length===0 && i!==0){ |
10 | + | if((i+1)%length===0 && i!==0){ |
11 | - | newArr.push(temp); |
11 | + | newArr.push(temp); |
12 | - | temp = []; |
12 | + | temp = []; |
13 | - | } |
13 | + | } |
14 | - | } |
14 | + | } |
15 | - | newArr.push(temp); |
15 | + | newArr.push(temp); |
16 | - | return newArr; |
16 | + | return newArr; |
17 | - | } |
17 | + | } |
18 | - | |
18 | + | |
19 | console.log(chunk(arr, 4)) |