jQuery Remove Value from Array [2 Ways]

/ / 0 Comments

Remove specific value from Array in jQuery: Here in this article, we see how to remove specific value from array in jquery. The traditional way to delete matched values from an array is by making forloop.

In forloop we check not matched values and add them into a new array variable.

In jQuery, we have a much more simple option by using the grep method and the $.inArray method we can remove specific value from array.

2 approaches to remove specific value from Array in jQuery:

  1. Using the grep method to remove value from array if exists.
  2. Using jQuery.inArray method to remove value from array. 

Output:

jquery remove value from array

#1 jQuery Code: Using grep method to remove value from Array

Here first we add an array variable with some values, as shown below. And now using the $.grep method we remove value 5 from our array variable.

The jQuery grep method is used to return an element that satisfies the filter function.

It means whatever condition we have written if it satisfies it will return those elements. Here in our case, our condition value!= 5, hence it will return all other array values, except value 5.

Our final code looks like as written below:

var arrNumber = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; // define array variable with some values 
var removeValue = 5; // remove item value from array 

arrNumber = $.grep(arrNumber, function(n) {
  return n != removeValue;
});

As in the above js code, we can remove an array element by value in jQuery successfully.

View Demo

#2: Use jQuery.inArray method to remove value from Array

Here we have one array variable having values as fruits name. And another variable that has value, which we need to remove from our array object.

The variable removeArrayValue has the value mango, which will remove from our array variable (arrFruits). Our final code looks like as written below:

var arrFruits = ["apple", "mango", "banana", "watermelmen", "tomato"];
var removeArrayValue = "mango";
arrFruits.splice($.inArray(removeArrayValue, arrFruits), 1);

Here in the above code, we use the javascript splice() method which is used to add or remove array elements. And jquery build in inArray method which is used to search for a specific value in an array and return its index.

View Demo

By using a combination of splice and jquery.inArray we can remove a specific element from an array variable.

Conclusion: Here we see 2 different approaches in jquery to remove specific elements from an array by given value. We have used grep and jquery.inArray method to remove objects from an array.

Other Reference:

Thank you for reading, pls keep visiting this blog and share this in your network. Also, I would love to hear your opinions down in the comments.

PS: If you found this content valuable and want to thank me? 👳 Buy Me a Coffee

Subscribe to our newsletter

Get the latest and greatest from Codepedia delivered straight to your inbox.


Post Comment

Your email address will not be published. Required fields are marked *

0 Comments