This repository was archived by the owner on Jan 13, 2022. It is now read-only.
-
Couldn't load subscription status.
- Fork 1
each
oatkiller edited this page Sep 13, 2010
·
7 revisions
use each to run a function on each item in an array. the function gets for its params the item, its index, and the array.
you can use this function in the take form to operate on non-array objects.
your iterator function can return o.each_break to break the iteration
(function () {
[1,2,3][o.each](function (n,index,my_array) {
// this will be 1, then 2, then 3.
// this is different than a for loop in that
// it runs an fn each time, so closure happens
alert(n);
});
// you can also use it on objects that aren't arrays
o.each({
name: 'Robert',
age: 23,
food: 'peanut butter'
},function (element,property_name,obj) {
alert(property_name + ': ' + element);
});
// you can break the loop by returning the special
// o.each_break object;
[1,2,3][o.each](function (n) {
if (n === 2) {
// the each will stop iterating now
return o.each_break;
}
});
})();