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

170
Views
How to make an algorithm for complex pagination in JS?

I'm trying to make pagination work but it's a bit complex for me so I need help. If someone has an idea how to make it, I would be thankful.

I need to show items that the user owns. One page can have maximum of 12 items displayed. I have a pageData array, that returns the number of items for a category. Category "shirts" has 2 items.

pageData = [ {id:'pants',totalItems: 15},
{id:'shirts',totalItems: 2}, {id:'dresses',totalItems: 13}]

On first load I need to show 12 items (lets say 12 pants), on second load (click on load more button) I need to show 3 more pants, 2 shirts and 7 dresses.

Metadata for an item has to be fetched via api (this is just an example how to get id of specific item, I'm trying to put it somehow into code but don't have clear idea how):

 for (let i = 0; i < pageData['pants'].totalItems; i++) {          
          const metadata = await api.getMetadata({
            userId: 'userId',
            id: i,
          });         
        }
 

This is some code that I have but like I said, I don't really have an idea how to make it:

  const getItems = async (pageData, currentPage) => {
    const itemsPerPage = 12;
    let fetchedData = [];
    let total = 0;
    let start = currentPage * itemsPerPage;

    for (let i = 0; i < pageData.length; i++) {
      const minRange = total;
      const maxRange = minRange + itemsPerPage;

      if (start >= minRange && start <= maxRange) {
        const minId = minRange - total;
        const maxId = maxRange - total;

        for (let j = minId; j < maxId; j++) {
          const metadata = await api.getMetadata({
            userId: 'userId',
            id: j,
          });
          fetchedData.push(metadata);
        }
      }
      total += itemsPerPage;
    }
  };
about 4 years ago · Juan Pablo Isaza
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!