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

160
Views
How to send n axios post requests depending on number of elements in array

I have a params object:

const params = {price: '5000', qty: ''}

I have an array:

const sizes = [2, 3, 4, 5]

How can I make a params object for each element in the array and then use each object to make an axios request with number of requests depending on the number of elements in the array?

The array will vary. Sometimes it will have 4 or elements, or 10, or 20. I think the structure should look like the below sample, but it will vary depending on the size of the array.

   const [
     { data: data1 },
     { data: data2 },
     { data: data3 },
     { data: data4 },
   ] = await Promise.all([
     axios.post(url, params), // const params = {price: '5000', qty: '2'}
     axios.post(url, params), // const params = {price: '5000', qty: '3'}
     axios.post(url, params), // const params = {price: '5000', qty: '4'}
     axios.post(url, params), // const params = {price: '5000', qty: '5'}
   ])
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

See {...params, qty: size}, this part makes qty equal to each item in the array. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map for more info on array mapping and https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax for info on the ... operator (or spread operator).

const sizes = [2, 3, 4, 5];
const response = await Promise.all(sizes.map(size => axios.post(url, {...params, qty: size}));
const datas = response.map(res => res.data); // [{data: ...}, {data: ...}];
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!