Javascript Array Prototype remove element

Introduction

Create an array remove function, that removes an element from given array


'use strict'//from   w  w  w.  j  a  v a  2 s  . c  om

Array.prototype.remove = function remove(index) {
  if(typeof index !== 'number' || index < 0 || index > this.length) return;

  this.splice(index, 1);
}

let myArray = [1, 2, 3];
myArray.remove(1);
console.log(myArray);

myArray.remove(1);
console.log(myArray);



PreviousNext

Related