I’m currently working on a small project where I want to firstly target a dynamic number of items and then assign values from an array to custom data attributes, matching the item and array row numbers at all times until the end of the array is reached so that the data will match up.
At the moment I have the targeting of the elements setup, an array and have attempted to loop through the items in the array using both a forEach and a standard for loop but I am hitting the same problem which is that only the values from the last row are being applied. I have figured out that this is because these are the last values assigned to the variables itemNumber and itemColour but I cannot figure out a way to fix it.
The only thing I can think of is to export the variable values somehow to then build out the generated HTML with them, then continue with the loop.
Instead of looping through each item, you already know which item you want - it’s the i’th item in the array. (That’s why you checked if i is less than the length of the array - so you know the ith item exists…)
You dont know the number of divs or items - whichever way around you do it, you need to make sure youre not moving outside the index of the array you’re not looping through.
I very much dislike Javascripts handling of array index referencing. The code there will work, but in pretty much every other language will cause problems. So to those that follow, be careful taking that approach to heart.
Yeah, the general programmer in me absolutely screams at the second one, because you reference a variable beyond the array bounds to set up for that check. Works in Javascript, but EVERYWHERE else, it’s going to bite you.