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

130
Views
Perform some action on each element of an array but in small portions using JavaScript

I'm trying to understand how to perform some action on each element of an array, but by working in portions of that array, until each element has been touched.

As a more specific example, let's assume I have an array of 990 elements and want to perform some action on each element, but in portions of 200. What would be the most efficient way to do this?

function foo(array) {
    results = []
    if (array.length > 200) {
        // Loop over and perform action on first 200 elements, then next 200, and so on...
        // for each element, push result to results array
    }

    return results;
}

EDIT:

For my specific use case, each element in the array is a URL. I'm making a GET request with each URL using Axios. There is potential for my array to contain thousands of URLs, so I don't want to make a request and wait for a response one at a time; however, the server I'm making the requests to can only handle so many requests at one time (about 200).

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

0

While it may not be the most efficient solution per my initial request, I found the following to be easy-to-understand:

while (array.length != 0) {
    array.splice(0, 200).forEach(function (url) {
        // Perform some action
    });
}
about 4 years ago · Juan Pablo Isaza Report

0

To make multiple chunks you can use reduce, like that:

var perChunk = 200 // chunk size

var inputArray = [] // your array

const result = inputArray.reduce((resultArray, item, index) => { 
  const chunkIndex = Math.floor(index/perChunk)

  if(!resultArray[chunkIndex]) {
    resultArray[chunkIndex] = [] // new chunk
  }

  resultArray[chunkIndex].push(item)

  return resultArray
}, [])

console.log(result);

Then you can iterate again over the sible chunks a make your axios request.


for(i=0; i< result.length; i++) {
  let delay = 3000 * i
  setTimeout(() => {
    console.log(// your action with the Array arr[i])
  }, delay)
}
about 4 years ago · Juan Pablo Isaza Report

0

There are lots of ways to do this. Some ways better than others. But I will assume you dont want to modify the original array and want to handle 200 elements on different moments:

function stepArray(arr){
   //... create your custom index on the array object
   //..., so it will know where to continue from
   if(typeof arr.myIndex == 'undefined'){ arr.myIndex = 0; }

   for(var k=0; k<200; k++){
      if(k + arr.myIndex >= arr.length){return;}
      process(arr[k + arr.myIndex]);
   }
}
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!