Created
September 11, 2020 14:48
-
-
Save lumixraku/7659bf7230ee94238b61900438a1522d to your computer and use it in GitHub Desktop.
Revisions
-
lumixraku created this gist
Sep 11, 2020 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,15 @@ const listChunk = (list, size = 1, cacheList = []) => { const tmp = [...list] if (size <= 0) { return cacheList } while (tmp.length) { cacheList.push(tmp.splice(0, size)) } return cacheList } console.log(listChunk([1, 2, 3, 4, 5, 6, 7, 8, 9])) // [[1], [2], [3], [4], [5], [6], [7], [8], [9]] console.log(listChunk([1, 2, 3, 4, 5, 6, 7, 8, 9], 3)) // [[1, 2, 3], [4, 5, 6], [7, 8, 9]] console.log(listChunk([1, 2, 3, 4, 5, 6, 7, 8, 9], 0)) // [] console.log(listChunk([1, 2, 3, 4, 5, 6, 7, 8, 9], -1)) // []