Nodejs Array Each each(method)

Here you can find the source of each(method)

Method Source Code

/**/* w w  w .  j ava2s .com*/
 * Adds an 'each' method to Array prototype.
 * The idea being that forEach is nice, but it's lame
 * to have the array reference passed as an argument
 * and also not being able to break out. This solves both problems
 *
 * @method Array.prototype.each
 * @param {function(this:Array [value, index, exit])} method Runs on all keys
 * @return {Array} Self reference for chaining
 */
Array.prototype.each = function(method) {
   var _handBreak = false;

   function _break() { _handBreak = true; }

   for (var i = this.length - 1; i >= 0; i--) {
      if (_handBreak) return this;
      method.call(this, this[i], i, _break); 
   }
   return this;
};

Related

  1. each(iterationFunction)
    Array.prototype.each = function(iterationFunction) {
        this.eachIndex(function(element, index) {
            iterationFunction(element);
        });
    };
    
  2. each(iterator)
    Array.prototype.each = function(iterator) {
      for(var i = 0; i < this.length; i++) {
        iterator(this[i], i);
    
  3. each(iterator, context)
    Array.prototype.each = function(iterator, context)
      for(var i = 0, length = this.length >>> 0; i < length; i++)
        if(i in this)
          if(iterator.call(context, this[i], i, this) === false)
            return;
    
  4. each(length, callback)
    Array.prototype.each = function(length, callback) {
      if (typeof length == 'function') {
        callback = length;
        length = this.length;
      for (var i=0; i < length; i++) {
        callback(this[i]);
    };
    ...
    
  5. each(method)
    Array.prototype.each = function(method) {
      for (var i = 0; i < this.length; i++) {
        this[i][method]()
    
  6. each(method)
    Array.prototype.each = function(method) {
      var args = Array.prototype.slice.call(arguments,1)
      for (var i = 0; i < this.length; i++) {
        this[i][method].apply(this[i],args)
    
  7. each(visitor)
    Array.prototype.each = function(visitor) {
      var iterator = this.getIterator();
      while (iterator.hasNext()) {
        var ret = visitor.call(this, iterator.next(), iterator.current);
        if (ret === false)
          break;
    
  8. eachIndex(iterationFunction)
    Array.prototype.eachIndex = function(iterationFunction) {
        for (var i = 0; i < this.length; i++) {
            iterationFunction(this[i], i);
    };
    
  9. eachRecursive(fieldName, filterOrDoFun, doFun)
    Array.prototype.eachRecursive = function(fieldName, filterOrDoFun, doFun) {
      var filter = doFun ? filterOrDoFun : null;
      doFun = doFun || filterOrDoFun;
      var doArrayRecursive = function(arr) {
        if (!arr || !(arr instanceof Array)) {
          return;
        for (var i = 0; i < arr.length; i++) {
          var item = arr[i];
    ...