Skip to content

Instantly share code, notes, and snippets.

@AitorAlejandro
Created April 16, 2021 15:11
Show Gist options
  • Save AitorAlejandro/c4bf8115a9c0722e9f592d48c3932165 to your computer and use it in GitHub Desktop.
Save AitorAlejandro/c4bf8115a9c0722e9f592d48c3932165 to your computer and use it in GitHub Desktop.
A way of creating a frontend pagination
// 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