Skip to content

Instantly share code, notes, and snippets.

@AlexPetrov7311
Created September 15, 2023 01:45
Show Gist options
  • Select an option

  • Save AlexPetrov7311/8cb3b853c644e55b61829b0a1e5a8b82 to your computer and use it in GitHub Desktop.

Select an option

Save AlexPetrov7311/8cb3b853c644e55b61829b0a1e5a8b82 to your computer and use it in GitHub Desktop.
Pagination array
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