e.g.
a = [12, 213, 321, 312, 32, 42]
and i want to remove 213 from it
but i don’t ever know what order it will be in, in the array
How can I select it from the array and then also remove it?
e.g.
a = [12, 213, 321, 312, 32, 42]
and i want to remove 213 from it
but i don’t ever know what order it will be in, in the array
How can I select it from the array and then also remove it?
Try this
array.splice(array.indexOf(213), 1);
Or if there is a possibility of having number which is not present in the array then you check it like this
var index = array.indexOf(213)
if(index > -1){
array.splice(index, 1);
}
You can use indexOf
method to get index of element and can use splice()
to remove that found element. eg:-
var array = a = [12, 213, 321, 312, 32, 42];
var index = array.indexOf(213);
//now remove this with splice method
if (index > -1) {
array.splice(index, 1);
}
You can use .splice() to remove the element, and use $.inArray() or Array.indexOf() to find the index of the element in the array
a = [12, 213, 321, 312, 32, 42]
a.splice($.inArray(a, 213), 1)
Note: Array.indexOf() was not used because of IE compatibility
You can find the index of the value with indexOf, then splice the array to remove the index.
Something like:
var idx = a.indexOf(213);
if (idx > -1) {
a.splice(idx, 1);
}
I think there are two ways to accomplish that:
The easier way is simply iterating over the array and pushing all the values in it except the one you want to delete to another array. Then you could redefine the previous array variable as the new array.
Another way could be to use the splice method, but I'm not familiar with it.