How to iterate over an array and delete elements in JavaScript

I have an array of elements and need to remove some of them. The problem is that JavaScript doesn't seem to have for every loop, and if I use a for loop, I run into problems with it, mainly trying to check elements outside the array or missing elements in the array, because the indexes are changing, Let show me what I mean:

var elements = [1, 5, 5, 3, 5, 2, 4]; for(var i = 0; i < elements.length; i++){ if(elements[i] == 5){ elements.splice(i, 1); } } 

The problem is that when the elements [1] are deleted, the elements [2] become the elements [1]. Therefore, the first problem is that some elements are never considered. Another problem is that .length changes, and if I hardcode the borders, then I can try to examine the elements outside the array. So what is the best way to make this incredibly easy?

+46
javascript iteration bounds
May 03 '13 at 5:57
source share
6 answers

Start from the top!

 var elements = [1, 5, 5, 3, 5, 2, 4]; for(var i = elements.length -1; i >= 0 ; i--){ if(elements[i] == 5){ elements.splice(i, 1); } } 
+123
May 3, '13 at 5:58
source share

You can use the filter method here:

 var elements = [1, 5, 5, 3, 5, 2, 4].filter(function(a){return a !== 5;}); //=> elements now [1,3,2,4] 

Or, if you do not want to touch elements :

 var elementsfiltered ,elements = [1, 5, 5, 3, 5, 2, 4] .filter( function(a){if (a!==5) this.push(a); return true;}, elementsfiltered = [] ); //=> elementsfiltered = [1,3,2,4], elements = [1, 5, 5, 3, 5, 2, 4] 

See MDN Documentation for filter

Alternatively, you can extend Array.prototype

 Array.prototype.remove = Array.prototype.remove || function(val){ var i = this.length; while(i--){ if (this[i] === val){ this.splice(i,1); } } }; var elements = [1, 5, 5, 3, 5, 2, 4]; elements.remove(5); //=> elements now [1,3,2,4] 
+20
May 03 '13 at 6:02
source share

 var elements = [1, 5, 5, 3, 5, 2, 4]; var i = elements.length; while (i--) { if (elements[i] == 5) { elements.splice(i, 1); } } console.log(elements); 
+2
Feb 27 '17 at 21:58
source share

Using Array.shift () :

 var array = [1, 2, 3, 'a', 'b', 'c']; while (array.length > 0) { console.log(array.shift()); } 

Edit: This is probably not up to the specifications. I misunderstood the question (remove only some elements) and was too impatient to add a method that was not mentioned yet ...

+1
Feb 23 '15 at 15:09
source share

You can simply reduce i whenever you delete an item.

 var elements = [1, 5, 5, 3, 5, 2, 4]; var l = elements.length; for(var i = 0; i < l; i++){ if(elements[i] == 5){ elements.splice(i, 1); i--; } } console.log(elements); 
+1
Oct 18 '16 at 2:42 on
source share

This is an example of using Array.indexOf , while and Array.splice to remove inline elements.

 var elements = [1, 5, 5, 3, 5, 2, 4]; var remove = 5; var index = elements.indexOf(remove); while (index !== -1) { elements.splice(index, 1); index = elements.indexOf(remove); } console.log(elements); 

Jsfiddle on

0
May 03 '13 at 6:12
source share



All Articles