Skip to content

Instantly share code, notes, and snippets.

@Falciighol
Created March 13, 2020 22:15
Show Gist options
  • Save Falciighol/ea7ad71e4b805c98fa142f324d2f7142 to your computer and use it in GitHub Desktop.
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
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