Pagination / array of arrays

PHOTO EMBED

Mon Aug 28 2023 21:14:16 GMT+0000 (Coordinated Universal Time)

Saved by @davidmchale #javascript #pagination #array #of #arrays

const paginate = (array) => {
  const itemsPerPage = 10;
  const numberOfPages = Math.ceil( array.length / itemsPerPage);

  // need to create an array of arrays [[10], [20], [30]] etc
  return Array.from({ length: numberOfPages }, (_, pageIndex) => {
    const startIndex = pageIndex * itemsPerPage;
    return array.slice(startIndex, startIndex + itemsPerPage);
  });

}

export default paginate
content_copyCOPY