Please check my code first
I used axios.all (GET method).
JavaScript
x
10
10
1
.then((res) => {
2
let everyDataArray = [];
3
4
console.log(res); // output = [ {response}, {response} ]
5
6
for(let i=0; i < res.length; i++) {
7
everyDataArray.push(res[i].response)
8
}
9
})
10
Right now, I’m using for loop for combining all the responses to an array.
However, What I’m trying to do is using forEach, not for.
I have checked https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach ,
but still working on how. Thank you.
Advertisement
Answer
Really what you want is to use map
(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map) because you want to transform one array into another.
To be clear, forEach
is for the case when you want to apply a function to each item to use for side effects (like printing), rather than collecting the return values.