Nodejs Array Slice slice(start, end)

Here you can find the source of slice(start, end)

Method Source Code

Array.prototype.slice = function(start, end) {
   if (end == null) {
      end = this.length;/*from   www  .  j  a v  a  2 s .c om*/
   }
   if (start < 0) {
      start = this.length + start;
   }
   if (end < 0) {
      end = this.length + end;
   }
   
   var results = new Array(end - start);
   for (var i = start, j = 0, len = this.length; i < len && i < end; i++, j++) {
      results[j] = this[i];
   }
   
   return results;
};

Related

  1. slice(index = 0)
    Array.prototype.slice = function(index = 0) {
      var arr = [];
      var length = this.length;
      if ( typeof index != "number" )
        throw 'TypeError';
      if ( length <= 0 )
        length = 0;
      for (index; index < length; index++)
        arr.push(this[index]);
    ...
    
  2. slice(index)
    Array.prototype.slice = function(index) {
      result = []
      if (typeof index != "number") {
        for (index; index < this.length; index++) {
          result.push(this[index])
      return result
    
  3. slice(start, end)
    Array.prototype.slice = function(start, end) {
        var arr = [];
        if (start === undefined && end === undefined) {
            for (var i=this.length;i--;arr.unshift(this[i]));        
        } else if (end === undefined) {
            for (var i = start; i < this.length; ++i) { arr.push(this[i]) }
        } else {
            for (var i = start; i < end && i < this.length; ++i) { arr.push(this[i]) }
        return arr;
    };
    
  4. sliceMe(begin, end)
    Array.prototype.sliceMe = function (begin, end) {
      if (arguments.length < 1) {
        begin = 0;
        end = this.length;
      } else if (arguments.length < 2) {
        end = this.length;
    
  5. sliceNonUnique()
    Array.prototype.sliceNonUnique = function() {
      var result = [], clone = this.sort();
      for (var i=0, l=clone.length; i<l; i++) {
        if (clone[i] === clone[i+1]) {
          var temp = [];
          while (clone[i] === clone[i+1]) { temp.push(clone[i]); i++; }
          if (clone[i] === clone[i-1]) { temp.push(clone[i]); }
          result.push(temp);
      return result;
    };
    
  6. sliceWhere(predicate)
    Array.prototype.sliceWhere = function(predicate)
      var slice = [];
      for (var i = 0; i < this.length; i++)
        if (predicate(this[i])) slice.push(this[i]);
      return slice;
    Array.prototype.filter = Array.prototype.sliceWhere;