Created
April 16, 2021 15:11
-
-
Save AitorAlejandro/c4bf8115a9c0722e9f592d48c3932165 to your computer and use it in GitHub Desktop.
A way of creating a frontend pagination
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
// here is an array of 120 items | |
const items = Array.from({length: 120}, (_, index) => index); | |
console.log("items ->", items); | |
function createPagination(items, itemsPerPage) { | |
const numberOfPages = Math.ceil(items.length / itemsPerPage); | |
return Array.from({length: numberOfPages}, (_, index) => { | |
const startIndex = index * itemsPerPage; | |
return items.slice(startIndex, startIndex + itemsPerPage); | |
}); | |
} | |
// the outcome is an array of arrays, that is what we wanted for a frontend pagination | |
const paginatedItems = createPagination(items, 14); | |
console.log("paginatedItems ->", paginatedItems); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment