Nodejs Array Push pushFront(value)

Here you can find the source of pushFront(value)

Method Source Code

// Given an array and additional value, insert this value at the beginning of the array.

// pushFront as a function that is not part of the Array prototype
// function pushFront(array, value) {
//    if (!Array.isArray(array)){
//       throw "1st Parameter not an array"
//    }//ww  w. j  a  v  a  2s. c o m

//    if (value === undefined) {
//       throw "2nd Parameter undefined"
//    }
//    for (var i = array.length; i > 0; i--) {
//       array[i] = array[i-1];
//    }
//    array[0] = value;
// }


// try {
//    array = [1,2,3,4,5]
//    pushFront(array,6)
// } catch(err) {
//    console.log("Error: ", err)
// }

// console.log("New array: ",array)


// Added pushFront function to the prototype of the Array object
Array.prototype.pushFront = function(value) {
   for(var i = this.length; i>0; i--){
      this[i] = this[i-1];
   }
   this[0] = value;
}

// Test cases
array = [1,2,3,4,5];
array.pushFront(0);
array.pushFront("Hello");
array.pushFront(false);
array.pushFront(function(value){
   console.log(value);
});

console.log(array);

Related

  1. pushArray(arr)
    Array.prototype.pushArray = function(arr) {
        this.push.apply(this, arr);
    };
    
  2. pushArray(arr)
    Array.prototype.pushArray = function(arr) {
      for (var i = 0; i < arr.length; ++i)
        this.push(arr[i]);
    
  3. pushArray(arr)
    Array.prototype.pushArray = function(arr) {
        this.push.apply(this, arr);
    };
    
  4. push_all(array)
    Array.prototype.push_all = function(array) {
        for (var index in array) {
            this.push(array[index]);
    
  5. pushMe(num)
    Array.prototype.pushMe = function (num) {
      this[this.length] = num;
      return this;
    };
    
  6. akaPush(value)
    Array.prototype.akaPush=function(value){
      var len = this.length;
      this[len] = value;
      len++;
      this.length = len;
      return len;
    
  7. cutAndPush(f,xs)
    'use strict';
    Array.prototype.cutAndPush = function(f,xs) {
      var i = 0;
      while (i < this.length) {
        if (f(this[i])) {
          xs.push(this[i]);
          this.splice(i,1);
        } else {
          i++;
    ...
    
  8. pushArrayIfNotExist(element)
    Array.prototype.pushArrayIfNotExist = function(element) { 
      if (!this.ifArrayInArray (element)) {
        this.push(element);
    };
    
  9. pushRange(_items)
    Array.prototype.pushRange = function (_items) {
        for (var l = 0; l < _items.length; l++) {
            this.push(_items[l]);
    };