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);
};
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);
};