The below function takes array element which needs to be removed as parameter as below

ary.remove(‘One’);
var ary = [‘One’, ‘Two’, ‘Three’];
Array.prototype.remove = function() {
    var what, a = arguments, L = a.length, ax;
    while (L && this.length) {
        what = a[--L];
        while ((ax = this.indexOf(what)) !== -1) {
            this.splice(ax, 1);
        }
    }
    return this;
};

The above method works perfectly works for java script array with strings in it.

For the below variable

var csvEmpIds = ‘1,2,3,4,5’;
arrEmpIds = csvEmpIds.split(‘,’);
ary.remove(2);

The above will not work as you should pass 2 as a string and split function creates
a array by splitting from one variable and putting as a strings in array.

so csvEmpIds.split(‘,’) will create array like below

arrEmpIds = ['1','2','3','4','5'];

In this case to remove element from array which you got by splitting come delimited value you should
do as below.

ary.remove('2');

To Remove array which is declared globally use the below function

var ary = ['three', 'seven', 'eleven'];
removeA(ary, 'seven');
function removeA(arr)
{
  var what, a = arguments, L = a.length, ax;
  
  while (L > 1 && arr.length) 
  {
    what = a[--L];
    while ((ax= arr.indexOf(what)) !== -1) 
    {
      arr.splice(ax, 1);
    }
   }

   return arr;
}

Leave a reply