Created
March 13, 2020 22:15
-
-
Save Falciighol/ea7ad71e4b805c98fa142f324d2f7142 to your computer and use it in GitHub Desktop.
[Array pagination] Slice an array with the page size and page number params #javascript #js
This file contains 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(array, page_size, page_number) { | |
// human-readable page numbers usually start with 1, so we reduce 1 in the first argument | |
return array.slice((page_number - 1) * page_size, page_number * page_size); | |
} | |
console.log(paginate([1, 2, 3, 4, 5, 6], 2, 2)); | |
console.log(paginate([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], 4, 1)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment