Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

299
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda