benposa.blogg.se

For each js
For each js










for each js for each js

So, now we wanna call a callback, right? So, how do we call a function? We haven't really talked about that too much yet, but we just refer by name. Okay, so I'm just saying, start at zero, keep looping until we get to that length and increment by one each time, that's what that means, cool. Okay, so what's that loop that we want to use for the arrays again? Anyone? I forgot the most important part, which you should do at the end of everything is to celebrate, not to self. Loop through array, okay, so we got that part. Great, so we got that, and then otherwise, else we can do that we can loop through object, right? Just knows for ourselves, right? But you're right, we do have this handy function array.is array, and we pass the list this is going to return true if the list is an array. Yeah, so remember this is not gonna work, this is a sad booth, sad face this won't work because arrays are objects that's always going to return false. > No, not a string, is array there's a function called is array. And then, if it's an object, we'll do the foreign loop, okay? How do we check if something is an array? So we're gonna check which was an array if it's an array, we'll loop through it with the other one with the loop with the i and the semicolons, that one, the numerical loop. Because of the numerical indices, objects work better with the forehand loop. Like we mentioned before, arrays work well with the for loop with the I in the semicolons. Alright, and then we need to make sure that it works with both objects and arrays, and so, for objects and arrays, the loop is you should use different loops. And then in each loop I ended there because for each loop I wanted to call the callback with a list item. So first we want to loop through the list. Awesome, so, I'm just gonna write some pseudocode out just so that we can think through it before diving in. > Let's start with our underscore, right? It's an object, and then we're gonna say, underscore dot each equals a function, what does that function take? Do you remember? What's the first argument? Transcript from the "forEach and _.each Solution" Lesson












For each js