Categories
JavaScript Answers

How to paginate JavaScript array?

Spread the love

To paginate JavaScript array, we use the slice method.

For instance, we write

const paginate = (array, pageSize, pageNumber) => {
  return array.slice((pageNumber - 1) * pageSize, pageNumber * pageSize);
};

console.log(paginate([1, 2, 3, 4, 5, 6], 2, 2));

to define the pagination function that calls array.slice with the start and end index of the chunk of the array to return.

The item at the end index itself is not included with the returned array.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *