1

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.

4 Answers4

4

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);
    }
}
jfriend00
  • 580,699
  • 78
  • 809
  • 825
3

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]
dandavis
  • 14,821
  • 4
  • 34
  • 35
1

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.

PabloWeb18
  • 405
  • 3
  • 9
  • OP is looking to remove elements with a specific value – dc5 Sep 04 '13 at 19:04
  • The code you added at the end in your edit won't work because when you remove an item, the array indexes change and you will skip evaluating some items. – jfriend00 Sep 04 '13 at 19:10
  • So you can use splice()! The name of this function, the parameter names and the results aren't obvious. My recommendation is to just use splice(index,length) – PabloWeb18 Sep 04 '13 at 19:22
1

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);
    }
}
progrenhard
  • 2,347
  • 2
  • 11
  • 14