Created
June 7, 2017 09:03
-
-
Save pramendra/9982eab8b1a0928b35485e1c11f232f7 to your computer and use it in GitHub Desktop.
Convert Array to Array chunks - ES6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const arrayToChunks = (arrayData, length = 2) => { | |
const ImmutateArrayData = [...arrayData]; | |
let chunks = []; | |
while(([...data] = ImmutateArrayData.splice(0, length)).length !== 0){ | |
chunks.push(data); | |
} | |
return chunks; | |
} | |
const ab = [1, 2, 3, 4, 5, 6, 7]; | |
console.log(arrayToChunks(ab)); | |
console.log(arrayToChunks(ab, 3)); | |
console.log(arrayToChunks(ab, 4)); | |
console.log(arrayToChunks(ab, 5)); | |
// https://jsbin.com/qonamap/2/edit?js,console |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment