Nodejs Array Shift shift()

Here you can find the source of shift()

Method Source Code

//array.shift()//from  w w  w .  ja v a2s  . c om
var a = ['a', 'b', 'c'];
var c = a.shift(); //
console.log('a : ' + a); // b,c
console.log('c : ' + c); // a

//shift can be implemented like this.
//The splice() method adds/removes items to/from an array, and returns the removed item(s).
//array.splice(start,deleteCount,item..)

Array.prototype.shift = function () {
    return this.splice(0, 1)[0];
};

var d = ['a', 'b', 'c'];
var e = d.shift(); // a
console.log('e : ' + e); // a
console.log('d : ' + d); // a

Related

  1. shift()
    Array.prototype.shift = function () {
       return this.splice(0, 1)[0];
    };
    
  2. shift()
    Array.prototype.shift = function() {
      var elem = this[0];
      for (var i = 0, len = this.length; i < len - 1; i++) {
        this[i] = this[i + 1];
      delete this[--this.length];
      return elem;
    };
    
  3. shift()
    Array.prototype.shift = function () {
        var firstUnit = this[0];
        for (var i = 0; i < this.length; i++)
            this[i] = this[i+1];
        this.pop();
        return firstUnit;
    
  4. shift()
    Array.prototype.shift = function() {
      for(var i = 0; i < this.length-1; i++) {
        this[i] = this[i+1];
      this.length--;
      return this[0];
    };
    
  5. shiftMe()
    Array.prototype.shiftMe = function () {
      var shifted = this[0];
      for (var i = 1; i < this.length; i++) {
        this[i-1] = this[i];
      this.splice(-1,1);
      return shifted;