add and remove array value in jquery?
I have array of data var y = [1, 2, 3];
I want to remove 2 from array y.
Any one have idea how to remove particular value from array using jquery? I have tried pop() but its working like stack.
add and remove array value in jquery?
I have array of data var y = [1, 2, 3];
I want to remove 2 from array y.
Any one have idea how to remove particular value from array using jquery? I have tried pop() but its working like stack.
You can do something like this:
var y = [1, 2, 3]
var removeItem = 2;
y = jQuery.grep(y, function(value)) {
return value != removeItem;
});
Result:
[1, 3]
by using simple javascript you can use this:
Array.remove('Valuetoberemoved');
ex: y.remove('2');
or this:
There is no native way to do this in Javscript. You could use a library or write a small function to do this instead: http://ejohn.org/blog/javascript-array-remove/
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()
.