javascript tutorial - [Solved-5 Solutions] Remove a Particular Element - javascript - java script - javascript array



Problem:

How to remove a particular element from an array in javascript ?

Solution 1:

First, find the index of the element you want to remove:

var array = [2, 5, 9];
var index = array.indexOf(5);
click below button to copy the code. By JavaScript tutorial team

Then remove it with splice:

if (index > -1) {
    array.splice(index, 1);
}

click below button to copy the code. By JavaScript tutorial team

The second parameter of splice is the number of elements to remove. Note that splice modifies the array in place and returns a new array containing the elements that have been removed.

If we need indexOf in an unsupported browser, try the following polyfill. Find more info about this polyfill here.

Array.prototype.indexOf || (Array.prototype.indexOf = function(d, e) {
    var a;
    if (null == this) throw new TypeError('"this" is null or not defined');
    var c = Object(this),
        b = c.length >>> 0;
    if (0 === b) return -1;
    a = +e || 0;
    Infinity === Math.abs(a) && (a = 0);
    if (a >= b) return -1;
    for (a = Math.max(0 <= a ? a : b - Math.abs(a), 0); a < b;) {
        if (a in c && c[a] === d) return a;
        a++
    }
    return -1
});
click below button to copy the code. By JavaScript tutorial team

Solution 2:

  • we don't know how you are expecting array.remove(int) to behave. There are three possibilities we can think of that you might be wanting.
  • To remove an element of an array at an index i:
array.splice(i, 1);
click below button to copy the code. By JavaScript tutorial team
  • If we want to remove every element with value number from the array:
for(var i = array.length - 1; i >= 0; i--) {
    if(array[i] === number) {
       array.splice(i, 1);
    }
}
click below button to copy the code. By JavaScript tutorial team

If we just want to make the element at index i no longer exist, but you don't want the indexes of the other elements to change:

delete array[i];
click below button to copy the code. By JavaScript tutorial team

Solution 3:

  • Depends on whether you want to keep an empty spot or not.
  • If we do want an empty slot, delete is fine:
delete array[ index ];
click below button to copy the code. By JavaScript tutorial team

If we don't, you should use the splice method:

array.splice( index, 1 );
click below button to copy the code. By JavaScript tutorial team

And if you need the value of that item, you can just store the returned array's element:

var value = array.splice( index, 1 )[0];
click below button to copy the code. By JavaScript tutorial team
  • In case you want to do it in some order, you can use array.pop() for the last one or array.shift() for the first one (and both return the value of the item too).
  • And if you don't know the index of the item, you can use array.indexOf( item ) to get it (in a if() to get one item or in a while() to get all of them). array.indexOf( item ) returns either the index or -1 if not found.
Remove ALL instances from an array
  function remove(arr, item) {
      for(var i = arr.length; i--;) {
          if(arr[i] === item) {
              arr.splice(i, 1);
          }
      }
  }
click below button to copy the code. By JavaScript tutorial team

It loops through the array backwards (since indices and length will change as items are removed) and removes the item if it's found. It works in all browsers.

Solution 5:

Array.prototype.remByVal = function(val) {
    for (var i = 0; i < this.length; i++) {
        if (this[i] === val) {
            this.splice(i, 1);
            i--;
        }
    }
    return this;
}
//Call like
[1, 2, 3, 4].remByVal(3);1
click below button to copy the code. By JavaScript tutorial team

Related Searches to javascript tutorial - Remove a Particular Element