close

[Solved] Paginate Javascript array

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error Paginate Javascript array in javascript. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How Paginate Javascript array Error Occurs?

Today I get the following error Paginate Javascript array in javascript.

How To Solve Paginate Javascript array Error ?

  1. How To Solve Paginate Javascript array Error ?

    To Solve Paginate Javascript array Error
    const page = 2 const step = 2; const start = page * step - step; const end = start + step; const array = [1,2,3,4,5,6] console.log(array.splice(start, end))

  2. Paginate Javascript array

    To Solve Paginate Javascript array Error
    const page = 2 const step = 2; const start = page * step - step; const end = start + step; const array = [1,2,3,4,5,6] console.log(array.splice(start, end))

Solution 1


Another aproach that you can utilize, is using .filter, look:

const paginate = function (array, index, size) {
        // transform values
        index = Math.abs(parseInt(index));
        index = index > 0 ? index - 1 : index;
        size = parseInt(size);
        size = size < 1 ? 1 : size;

        // filter
        return [...(array.filter((value, n) => {
            return (n >= (index * size)) && (n < ((index+1) * size))
        }))]
    }

var array = [
  {id: "1"}, {id: "2"}, {id: "3"}, {id: "4"}, {id: "5"}, {id: "6"}, {id: "7"}, {id: "8"}, {id: "9"}, {id: "10"}
 ]


var transform = paginate(array, 2, 5);

console.log(transform) // [{"id":"6"},{"id":"7"},{"id":"8"},{"id":"9"},{"id":"10"}] 

Solution 2

Hey I’m sorry I’m a bit late but we can use the Array.splice(start, end) method except this is much simpler

const page = 2
const step = 2;
const start = page * step - step;
const end = start + step;

const array = [1,2,3,4,5,6]
console.log(array.splice(start, end))

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also Read

Leave a Comment