The foreach statement repeats a group of embedded statements for each element in an array or an object collection. The foreach statement is used to iterate through the collection to get the information that you want.
It is not be used to add or remove items from the source collection to avoid unpredictable side effects. (If you need to add or remove items from the source collection, use a for loop.)
So my C# code looks like this:
0: 0 1: 1 2: 1 3: 2 4: 3 5: 5 6: 8 7: 13
jQuery Foreach Loops
jQuery makes the concept of a foreach loop simpler. To iterate over an array in jQuery, use the jQuery.each() (or in shorthand, $.each()) function.
$.each() function can be used to iterate over any collection, whether it is an object or an array.
Note: This is a different function than the more commonly known $(whatever_element).each(), which you use to iterate over a jQuery object.
0 1 1 2 3 5 8 13
In the case of an array, the callback is passed an array index and a corresponding array value each time. (The value can also be accessed through the
this value as an
Object even if it is a simple string or number value.)
You can break the
$.each() loop at a particular iteration by making the callback function return
false. Returning non-false is the same as a
continue statement in a for loop; it will skip immediately to the next iteration.
_.each(list, iteratee, [context]) Alias: forEach
0 1 1 2 3 5 8 13 "1: one" "2: two" "3: three"
jQuery.each() in the jQuery API Documentation
foreach, in (C# Reference) on MSDN