Nodejs Array Take take(howMany, f)

Here you can find the source of take(howMany, f)

Method Source Code

Array.prototype.take = function(howMany, f){
   var counter = 0;
   var i = 0;//www  .ja v a 2 s . c  om
   var addIt = false;
   var newArray = [];

   while(counter < howMany && i < this.length){

       if(f == undefined){
           addIt = true;
       }else{
           if(f(this[i]) == true){
               addIt = true;
           }
       }
       if(addIt){
           newArray.push(this[i]);
           counter++;
           addIt = false;
       }
       i++;
   }

   return newArray;
};

Related

  1. take(c)
    Array.prototype.take = function (c) {
      return this.slice(0, c);
    };
    
  2. take(count)
    Array.prototype.take = function (count) {
        var result = new Array();
        for (var i = 0; i < count && i < this.length; i++)
            result.push(this[i]);
        return result;
    };
    
  3. take(elements)
    Array.prototype.take = function(elements) {
        if (elements > this.length) {
            return [];
        } else {
            return this.slice(0, elements)
    
  4. take(length)
    Array.prototype.take = function (length) {
      var result = new Array();
      length = Math.min(this.length, length);
      for (var i = 0; i < length; i++)
        result.push(this[i]);
      return result;
    };
    
  5. take(n)
    Array.prototype.take = function (n) {
      return this.splice(0, n);
    };
    
  6. take(x)
    Array.prototype.take = function(x){
      return this.slice(0,x)
    
  7. takeSample()
    Array.prototype.takeSample = function () {
        var n = Math.round((Math.random() * (this.length - 1)));
        return this[n];
    };