Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

302
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda