Nodejs Array Collect collect( func )

Here you can find the source of collect( func )

Method Source Code

Array.prototype.collect = function( func ) {
   if( $.isFunction(func) ) {
      var _arr = [];
      for( var i=0; i<this.length; i++ ) {
         var item = this[i];
         var o = func(item);
         if(o!=null) _arr.push(o);
      }/* w w  w  .  ja  va  2 s  .c o m*/
      return _arr;
   }
   else {
      console.log("Please pass a function when using collect","Error");
   }
};

Related

  1. collect(collectFunction)
    Array.prototype.collect = function(collectFunction) {
        return this.inject([], function(result, element) {
            result.push(collectFunction(element));        
            return result;
        });
    };
    
  2. collect(fn)
    Array.prototype.collect = function(fn) { 
      var result = [];
      for (var i = 0; i < this.length; i += 1) {
        result.push(fn(this[i]));
      return result;
    };
    
  3. collect(fn)
    Array.prototype.collect = function(fn) {
      var a = [];
      for(var i = 0; i < this.length; i++) {
        a.push(fn(i, this[i]));
      };
      return a;
    };
    
  4. collect(fun)
    Array.prototype.collect = function(fun) {
        var result = [];
        this.each(function(it) {
            result.push(fun(it));
        });
        return result;
    };