Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

229
Views
How to separate my array into some parts in discord.js?

I'm doing pagination in an embed and I wanted to split 4 elements from an array for each page. How do I do this? (to update when I click the button). Until then I have only the first step which is to get the first 4 elements, from the first page, with:

.slice(0, 4)

How do I get the next 4, and the next, and so on...

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

you need to define:

  • currentPage variable (set to 1 as default value)
  • pageSize - how many items should be for one page
  • countPages - for calculation, rounded in bigger side

I have written 3 methods

  • loadFirstPage()
  • loadNextPage()
  • loadPrevPage()

const input = [1,2,3,4,5,6,7,8,9,10,11,12,13];
let currentPage = 1;
let pageSize = 4;
const countPages = Math.ceil(input.length / pageSize);


function loadFirstPage() {
    currentPage = 1;
    return [...input].splice(0, pageSize)
}

function loadNextPage() {
    const nextPage = currentPage + 1;
    if (nextPage <= countPages) {
        currentPage = nextPage;
        const startIndex = currentPage * pageSize - pageSize;
        return [...input].splice(startIndex, pageSize)
    } else {
        currentPage = countPages;
        const startIndex = currentPage * pageSize - pageSize;
        return [...input].splice(startIndex, pageSize)
    }
}

function loadPrevPage() {
    const prevPage = currentPage - 1;
    if (prevPage >= 1) {
        currentPage = prevPage;
        const startIndex = currentPage * pageSize - pageSize;
        return [...input].splice(startIndex, pageSize)
    } else {
        currentPage = 1;
        const startIndex = currentPage * pageSize - pageSize;
        return [...input].splice(startIndex, pageSize)
    }
}

console.log('output data: ', loadFirstPage(), 'current page: ', currentPage);

console.log('output data: ', loadNextPage(), 'current page: ', currentPage);
console.log('output data: ', loadNextPage(), 'current page: ', currentPage);
console.log('output data: ', loadNextPage(), 'current page: ', currentPage);
console.log('output data: ', loadNextPage(), 'current page: ', currentPage);

console.log('output data: ', loadPrevPage(), 'current page: ', currentPage);
console.log('output data: ', loadPrevPage(), 'current page: ', currentPage);
console.log('output data: ', loadPrevPage(), 'current page: ', currentPage);
console.log('output data: ', loadPrevPage(), 'current page: ', currentPage);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!