When you go ahead and click the button to run the code, we can see four lines of output corresponding to each item in the cars array. This function gets executed on each loop through the array, on each element of the array. The second parameter to the each call is our iterator function. The way we do this is to call the each function and pass in the cars array as the first parameter. Let’s see if we can iterate over each car in the cars array to do something interesting. In this first example we have a simply array of cars. The each function is really one of those bread and butter type functions that you just have to know, so let’s dig in now and get better at working with each in Underscore.įirst off, we’ll have a look at working with a simple array with the each function. When we say with context, all we really mean is what the this keyword will be pointing to during particular portions of code execution. In this particular tutorial we’ll have a look at the each function and how we can use it with arrays, objects, arrays with context, as well as objects with context. As we mentioned, the various collection functions in Underscore really help us to navigate our way around JavaScript objects and arrays in a really easy and straightforward way.
0 Comments
Leave a Reply. |