How can I Improve my logic by using forEach, not for loop in Javascript


Please check my code first

I used axios.all (GET method).

.then((res) => {
  let everyDataArray = [];
  console.log(res); // output = [ {response}, {response} ]    

  for(let i=0; i < res.length; i++) {

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 ,

but still working on how. Thank you.


Really what you want is to use 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.

Answered By – ndc85430

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published