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

301
Views
For loop is not storing the data correctly, where am I making the mistake?

In the code below I am looping over results.data.data.length and I am getting the correct data and storing it correctly as well.

The data:

param1 = [6, 27, 34, 22, 23, 25, 28, 24, 26, 30, 29] // => array length 11

Now the part that I am having an issue with, in the second loop I am getting the correct output for EnrolledNum and I am getting 11 numbers outputted but individually. So far so good.

Here is the issue, when I try to store the values into the empty array var EnrolledNum = [] it's only storing the last number. I am console.log inside and outside the loop to make sure things are ok. The one inside the loop is logging it individually correctly but the outside the loop is showing me an array of length = 1 which is the last number. What is the issue here?

fetchInitialData = async () => {
    this.setGlobal({ loading: true });
    const ep = `${process.env.REACT_APP_API}/partners/programs/list`;
    const results = await axios.get(ep);
    console.log("results data", results);

    var param1 = [];
    for (let i = 0; i < results.data.data.length; i++) {
      param1.push(results.data.data[i].id);
    }
    console.log("Param....", param1);


    for (let j = 0; j < param1.length; j++) {
      console.log('chekcingJ',j,'CheckingParam#', param1[j])

      const customers_ep = `${process.env.REACT_APP_API}/partners/programs/customers/${param1[j]}`;
      const customers = await axios.get(customers_ep);
      console.log("customers....", customers);

      var EnrolledNum = [];
      EnrolledNum.push(
        customers.data.data.filter(
          e =>
            e.status_stage === "Accepted_Paid" ||
            e.status_stage === "Accepted_Manual"
        ).length
      );

      console.log("In the Loop: EnrolledNumber...", EnrolledNum);
    }

    console.log("Outside the Loop: EnrolledNumber...", EnrolledNum);
  };
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You have to move the var EnrolledNum = []; outside of the forloop

Otherwise, you are re creating the variable each time it enters in the loop, and creates it as an empty array.

Try this out:

etchInitialData = async () => {
    this.setGlobal({ loading: true });
    const ep = `${process.env.REACT_APP_API}/partners/programs/list`;
    const results = await axios.get(ep);
    console.log("results data", results);

    var param1 = [];
    for (let i = 0; i < results.data.data.length; i++) {
      param1.push(results.data.data[i].id);
    }
    console.log("Param....", param1);

    var EnrolledNum = [];
    for (let j = 0; j < param1.length; j++) {
      console.log('chekcingJ',j,'CheckingParam#', param1[j])

      const customers_ep = `${process.env.REACT_APP_API}/partners/programs/customers/${param1[j]}`;
      const customers = await axios.get(customers_ep);
      console.log("customers....", customers);

      EnrolledNum.push(
        customers.data.data.filter(
          e =>
            e.status_stage === "Accepted_Paid" ||
            e.status_stage === "Accepted_Manual"
        ).length
      );

      console.log("In the Loop: EnrolledNumber...", EnrolledNum);
    }

    console.log("Outside the Loop: EnrolledNumber...", EnrolledNum);
  };
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!