He visto por ahí esta librería de JavaScript y lo comparto con todos.
Ejemplo de esta librería:
_.each([1, 2, 3], function(num, i) { equal(num, i + 1, 'each iterators provide value and iteration count'); }); var answers = []; _.each([1, 2, 3], function(num){ answers.push(num * this.multiplier);}, {multiplier : 5}); equal(answers.join(', '), '5, 10, 15', 'context object property accessed'); answers = []; _.forEach([1, 2, 3], function(num){ answers.push(num); }); equal(answers.join(', '), '1, 2, 3', 'aliased as "forEach"'); answers = []; var obj = {one : 1, two : 2, three : 3}; obj.constructor.prototype.four = 4; _.each(obj, function(value, key){ answers.push(key); }); equal(answers.join(", "), 'one, two, three', 'iterating over objects works, and ignores the object prototype.'); delete obj.constructor.prototype.four; var answer = null; _.each([1, 2, 3], function(num, index, arr){ if (_.include(arr, num)) answer = true; }); ok(answer, 'can reference the original collection from inside the iterator'); answers = 0; _.each(null, function(){ ++answers; }); equal(answers, 0, 'handles a null properly');
Recursos:
- GitHub del proyecto: enlace.