Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 66ab34e

Browse filesBrowse files
committed
[ADD] ChunkArray added
1 parent 2584972 commit 66ab34e
Copy full SHA for 66ab34e

File tree

Expand file treeCollapse file tree

2 files changed

+31
-0
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+31
-0
lines changed

‎README.md

Copy file name to clipboardExpand all lines: README.md
+22Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,4 +346,26 @@ getIndexToIns([2, 5, 10], 15) should return a number.
346346
getIndexToIns([], 1) should return 0.
347347

348348
getIndexToIns([], 1) should return a number.
349+
```
350+
351+
352+
## Chunk Array in Groups
353+
354+
Write a function that splits an array (first argument) into groups the length of size (second argument) and returns them as a two-dimensional array.
355+
356+
```javascript
357+
chunkArrayInGroups(["a", "b", "c", "d"], 2) should return [["a", "b"], ["c", "d"]].
358+
359+
chunkArrayInGroups([0, 1, 2, 3, 4, 5], 3) should return [[0, 1, 2], [3, 4, 5]].
360+
361+
chunkArrayInGroups([0, 1, 2, 3, 4, 5], 2) should return [[0, 1], [2, 3], [4, 5]].
362+
363+
chunkArrayInGroups([0, 1, 2, 3, 4, 5], 4) should return [[0, 1, 2, 3], [4, 5]].
364+
365+
chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6], 3) should return [[0, 1, 2], [3, 4, 5], [6]].
366+
367+
chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 4) should return [[0, 1, 2, 3], [4, 5, 6, 7], [8]].
368+
369+
chunkArrayInGroups([0, 1, 2, 3, 4, 5, 6, 7, 8], 2) should return [[0, 1], [2, 3], [4, 5], [6, 7], [8]].
370+
349371
```

‎src/chunckArray.js

Copy file name to clipboard
+9Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
function chunkArrayInGroups(arr, size) {
2+
const newArr = [];
3+
while(arr.length){
4+
newArr.push(arr.splice(0,size));
5+
}
6+
return newArr;
7+
}
8+
9+
console.log(chunkArrayInGroups(["a", "b", "c", "d"], 2));

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.