In my Twitch API project, I am able to display all online streamers, but for the offline streamers, I can only display the last user in my array. I have tried .push() to fix this, but it has not helped. From reading elsewhere, I know the issue is involving the scope of my variables, but I don’t understand it enough to fix my problem. I have taken a look at this FCC forum question, but the advice given there didn’t help my situation either. Would someone mind taking a look and help explain to me where I’m going wrong? Thanks.
Try to wrap your code in a forEach loop instead of a normal for loop, this usually fixes the closure issue. Hopefully this works.
Thanks! Right now it keeps returning “undefined”, but I will keep trying with the forEach loop.
It successfully pushed the array into my copy variable, but I honestly am not understanding it very well. I’m trying to figure out how to then iterate through that new array within the forEach loop, if that makes sense.
Have you updated your codepen? What new array in the for each loop?
EDIT: I see what you mean. Revert back to the old code. you dont need a copy array.
The i in the for each is not the index. In the docs it’s the (element, index) passed to the callback. So it is copy[element] which is undefined
There’s just something I’m not getting. I will keep reading up on forEach loops until it clicks
Open up the console to see the log
Thanks for your help!