How to remove specific value from array using jQuery
A working JSFIDDLE
You can do something like this:
var y = [1, 2, 2, 3, 2]var removeItem = 2;y = jQuery.grep(y, function(value) { return value != removeItem;});
Result:
[1, 3]
http://snipplr.com/view/14381/remove-item-from-array-with-jquery/
With jQuery, you can do a single-line operation like this:
Example: http://jsfiddle.net/HWKQY/
y.splice( $.inArray(removeItem, y), 1 );
Uses the native .splice()
and jQuery's $.inArray()
.
jQuery.filter
method is useful. This is available for Array
objects.
var arr = [1, 2, 3, 4, 5, 5];var result = arr.filter(function(elem){ return elem != 5; });//result -> [1,2,3,4]
http://jsfiddle.net/emrefatih47/ar0dhvhw/
In Ecmascript 6:
let values = [1,2,3,4,5];let evens = values.filter(v => v % 2 == 0);alert(evens);