I am having trouble understanding how my foreach loop is operating.
My goal is to iterate through 1 array of strings and add the current iterations string to the src attribute of an audio object in another array, such that each audio object has a matching, unique string. They are all mp3 files that represent different sounds.
Here is what I have
const srcArr = ['sounds/crash.mp3', 'sounds/kick-bass.mp3', 'sounds/snare.mp3', 'sounds/tom-1.mp3', 'sounds/tom-2.mp3',
'sounds/tom-3.mp3', 'sounds/tom-4.mp3'];
const audios = new Array(srcArr.length).fill(new Audio());
audios.forEach((audio, index) => {
audio.src = srcArr[index];
});
When I run this, however, it seems that on each iteration, it is adding the audio path string to every single audio element in the audios
array instead of the current audio object in the current forEach loop iteratio.
I feel as if I'm fundamentally misunderstanding something with the forEach loop.
My understanding of how I'm declaring it is that I am for looping over each audio object in the audios array, using an anonymous callback funcion that is being passed the audio object and index of that object as parameters. From there, it should simply be an assignment to the .src attribute of the object.
Could someone explain where I may be going wrong?
Thank you for the help in advance.