Reputation: 53
If I have a javascript array of numbers
[1, 2, 5, 7, 5, 4, 7, 9, 2, 4, 1]
And I want to search through that array and remove a particular number like 4 giving me
[1, 2, 5, 7, 5, 7, 9, 2, 1]
What's the best way to do that
I was thinking it might look like
for(var i = 0; i < myarray.length; i++) {
if(myarray[i] == 4) {
myarray.remove(i)
}
}
But there is no remove
function for an array. Also if I remove an element from the array it messes up my i
unless I correct it.
Upvotes: 5
Views: 4361
Reputation: 9
I prefer do something like this:
removeEmail(event){
myarray.splice(myarray.indexOf(event.target.id), 1)
}
myaraay.splice() Going to remove, myarray.indexOf() gives the number or whatever you wan to remove from inside the array. Thats the simplest way without need a loop over. :)
Upvotes: 1
Reputation: 2363
Here is a remove function based on index
function remove(array, index){
for (var i = index; i < arr.length-1; i++) {
array[i] = array[i+1];
}
}
Basically, what this does is shifts all the elements from the index to the "left." Not quite sure how splice works, but i'm guessing it works exactly the same way.
After adding that function to your code all you have to do is.
for(var i = 0; i < myarray.length; i++) {
if(myarray[i] == 4) {
remove(myarray,i);
}
}
Upvotes: 1
Reputation: 16726
personally, i like to use a re-usable function with the filter method:
//generic filter:
function without(a){return this!=a;}
//your data:
var r= [1, 2, 5, 7, 5, 4, 7, 9, 2, 4, 1];
//your data filtered against 4:
var no4=r.filter(without, 4);
//verify no 4s:
alert(no4); //shows: "1,2,5,7,5,7,9,2,1"
if you want this to mutate the original array, you can just wipe and push the new values into old array:
function without(a){return this!=a;}
var r= [1, 2, 5, 7, 5, 4, 7, 9, 2, 4, 1], //orig
r2=r.slice(); //copy
r.length=0; //wipe orig
[].push.apply( r, r2.filter(without, 4)); //populate orig with filtered copy
r; // == [1, 2, 5, 7, 5, 7, 9, 2, 1]
Upvotes: 6
Reputation: 411
John Resig, creator of jQuery created a very handy Array.remove method that I always use it in my projects.
// Array Remove - By John Resig (MIT Licensed)
Array.prototype.remove = function(from, to) {
var rest = this.slice((to || from) + 1 || this.length);
this.length = from < 0 ? this.length + from : from;
return this.push.apply(this, rest);
};
So, you can use your code like this:
// Remove the second item from the array
myarray.remove(1);
// Remove the second-to-last item from the array
myarray.remove(-2);
// Remove the second and third items from the array
myarray.remove(1,2);
// Remove the last and second-to-last items from the array
myarray.remove(-2,-1);
---Edit----
for(var i = 0; i < myarray.length; i++) {
if(myarray[i] == 4) {
myarray.remove(i);
}
}
Use your code like this to remove specific value.
Upvotes: 1
Reputation: 707198
You can use .splice()
to remove one or more items from an array and if you iterate from back to front of the array, your indexing doesn't get messed up when you remove an item.
var arr = [1, 2, 5, 7, 5, 4, 7, 9, 2, 4, 1];
for (var i = arr.length - 1; i >= 0; i--) {
if (arr[i] == 4) {
arr.splice(i, 1);
}
}
Upvotes: 8