-6

my program displays images using forEach. I want to replace it with for

  images.forEach((img, ind) =>  {
    const oImage = new Image();
        oImage.onload = () => 
        {
          ctx.drawImage(oImage, ind * 100, 0, 100, 100);
        };
    oImage.src = img.textContent;
  }
  );
}
  • 2
    Sharing your research helps everyone. Tell us what you've tried and why it didn’t meet your needs. This demonstrates that you’ve taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer! See also: [ask] – Ruben Helsloot Oct 24 '20 at 17:07
  • Why do you want to do this in the first place? – Barmar Oct 24 '20 at 17:09
  • to better understand the "forEach" function I did not understand well – stargate sg1 Oct 24 '20 at 17:31
  • @stargatesg1 I recommend you to study the use of forEach since it could be more easier to use than a traditional for. See the different ways [you can iterate over an array](https://stackoverflow.com/a/3010848/7927192) – ricardo-dlc Oct 24 '20 at 17:47
  • 2
    There is no actual question in your post – allenski Oct 24 '20 at 18:45

1 Answers1

1

I don't understand why you want to get rid of forEach, but you have the answer and it's simple as this:

for (let i = 0; i < images.length; i++) {
  const oImage = new Image();
  oImage.onload = () => {
    ctx.drawImage(oImage, i * 100, 0, 100, 100);
  };

  oImage.src = images[i].textContent;
}
ricardo-dlc
  • 466
  • 1
  • 3
  • 9