-
-
Save AlexPetrov7311/8cb3b853c644e55b61829b0a1e5a8b82 to your computer and use it in GitHub Desktop.
Pagination array
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
| function paginate (arr, size) { | |
| return arr.reduce((acc, val, i) => { | |
| let idx = Math.floor(i / size) | |
| let page = acc[idx] || (acc[idx] = []) | |
| page.push(val) | |
| return acc | |
| }, []) | |
| } | |
| let array = [1, 2, 3, 4, 5] | |
| let page_size = 2 | |
| let pages = paginate(array, page_size) | |
| console.log(pages) // all pages | |
| console.log(pages[1]) // second page |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment