By 05/02/2014 14 Comments

How to Remove elements of an array by value in Javascript

Remove an array element(s) by value

  • We can remove array element by given value

splice() function

  • This function adds / removes elements from an array
  • This function effects to original array and also change it

Following code snippet will guide you for remove elements of an array by value in Javascript


function removeByValue(arr, val) { // create function pass 2 parameters, first is an array and second is array's value (which will be removed)

for(var i=0; i if(arr[i] == val) { // loop through each array element and also pass if condition inside for loop for matching the array index and given value

arr.splice(i, 1); // call slice() function by an array, pass 2 parameters, first is array's index and second parameter is a number of element(s) to be removed from an array, if you pass 0 as a second parameter than no any element will be removed

break; // this statement exit from for loop

var somearray = ["mon", "tue", "wed", "thur"] // an array initialization

removeByValue(somearray, "tue"); // call function pass 2 parameters, first is an array and second is the value (which will be removed)

//output : "mon", "wed", "thur"


A method (removeByValue in example) has to be defined for removing elements by their value. New functions can be defined to classes at runtime in Javascript using prototypes.

In the code fragment given below, a removeByValue() method has been created inside the Array class.

Array.prototype.removeByValue = function(val) {
for(var i=0; i if(this[i] == val) {
this.splice(i, 1);

var somearray = ["mon", "tue", "wed", "thur"]


//somearray will now have "mon", "wed", "thur"


Posted in: JavaScript

Leave your valuable Comment

Have a natural attraction for women cosmetics and replica watches uk clothes, no
more than two for men the most attractive one, is to make their own driving experience, happy and can serve as the facade of the car, another is to highlight the taste edify sentiment rolex replica watch. The replica rolex is undoubtedly the most fashionable accessories, wear a suit to attend the activities, but also get a decent match on the replica watches .