Nodejs Array Each each(fn)

Here you can find the source of each(fn)

Method Source Code

/**//w w  w .j  a va2  s  . co m
 * Loops through each item and applies it to a callback function.
 *
 * @since   1.0
 * @param   function  fn  A function to apply each array item
 * @return  mixed     Returns the array or a return value from within
 *                    the callback function
 */
Array.prototype.each = function (fn) {
   var i = 0, length = this.length, ret;
   fn = typeof fn === 'function' ? fn : function () {};
   for (; i < length; i++) {
      ret = fn.call(this[i], i, this[i]);
      if (ret === false || ret !== undefined && ret !== true) {
         return ret;
      }
   }
   return this;
};

Related

  1. each(f, callback)
    var crypto = require('crypto')
    var http = require('http');
    Array.prototype.each = function(f, callback) {
        for (var i = 0, l = this.length; i < l; i++) {
            f(this[i]);
        if (callback) callback();
    Array.prototype.asyncEach = function(f, callback, i) {
    ...
    
  2. each(f,r)
    Array.prototype.each = function(f,r){
      if(r===-1){
        for(var i=this.length-1;i>=0;i--)f(this[i],i);
      }else{
        for(var i=0,l=this.length;i<l;i++)f(this[i],i);
    };
    
  3. each(fn)
    Array.prototype.each = function(fn) {
        if (!fn) return false;
        for(let i = 0, len = this.length; i < len; i++) {
            if (fn(this[i], i) === false) return false;
    
  4. each(fn)
    Array.prototype.each = function(fn) {
      for (var i = 0; i < this.length; i++) fn(this[i]);
    
  5. each(fn)
    Array.prototype.each = function(fn)
      for (var i=0; i<this.length; i++)
        fn.apply(this[i], [i]);
    };
    
  6. each(fn)
    Array.prototype.each = function(fn){
        fn = fn || Function.K;
         var a = [];
         var args = Array.prototype.slice.call(arguments, 1);
         for(var i = 0; i < this.length; i++){
             var res = fn.apply(this,[this[i],i].concat(args));
             if(res != null) a.push(res);
         return a;
    ...
    
  7. each(fn)
    Array.prototype.each = function(fn) {
      for(var i = 0; i < this.length; i++) {
        fn(i, this[i]);
      };
    };
    
  8. each(fn, last)
    Array.prototype.each = function(fn, last) {
        if (typeof fn != "function") return false;
        for (var index = 0; index < this.length; index++) {
                fn(this[index], index);
        if (last) last();
    };
    
  9. each(fnCallback)
    Array.prototype.each = function(fnCallback) {
        var value = null;
        for (var elementIndex in this) {
            if (this.hasOwnProperty(elementIndex)) {
                value = fnCallback.apply(this[elementIndex], [elementIndex, value]);
        return (value) ? value : this;
    };
    ...