Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splicing a Javascript array from within the callback passed to forEach

I have this code which is supposed to iterate over each item in an array, removing items based on some condition:

//iterate over all items in an array //if the item is "b", remove it.  var array = ["a", "b", "c"];  array.forEach(function(item) {   if(item === "b") {     array.splice(array.indexOf(item), 1);   }    console.log(item); });

Desired output:

a b c 

Actual output:

a b 

Obviously the native forEach method doesn't check after each iteration whether the item has been deleted, so if it is then the next item is skipped. Is there a better way of doing this, aside from overriding the forEach method or implementing my own class to use instead of an array?

Edit - further to my comment, I suppose the solution is to just use a standard for loop. Feel free to answer if you have a better way.

like image 958
Gus Avatar asked Feb 16 '14 13:02

Gus


People also ask

Can you modify array with forEach JavaScript?

forEach does not modify the array itself, the callback method can. The method returns undefined and cannot be chained like some other array methods. forEach only works on arrays, which means you need to be a little creative if you want to iterate over objects.

How JavaScript splice works in array?

splice() JS Array Method. The splice() method is a built-in method for JavaScript Array objects. It lets you change the content of your array by removing or replacing existing elements with new ones. This method modifies the original array and returns the removed elements as a new array.

How do you splice an element from an array?

Find the index of the array element you want to remove using indexOf , and then remove that index with splice . The splice() method changes the contents of an array by removing existing elements and/or adding new elements. The second parameter of splice is the number of elements to remove.

What is difference between array splice () and array slice () method in JavaScript?

The splice() method returns the removed items in an array. The slice() method returns the selected element(s) in an array, as a new array object. The splice() method changes the original array and slice() method doesn't change the original array.


1 Answers

Lets see why JavaScript behaves like this. According to the ECMAScript standard specification for Array.prototype.forEach,

when you delete an element at index 1, the element at index 2 becomes the element at index 1 and index 2 doesn't exist for that object.

Now, JavaScript looks for element 2 in the object, which is not found, so it skips the function call.

That is why you see only a and b.


The actual way to do this, is to use Array.prototype.filter

var array = ["a", "b", "c"];  array = array.filter(function(currentChar) {     console.log(currentChar);   // a, b, c on separate lines     return currentChar !== "b"; }); console.log(array);             // [ 'a', 'c' ] 
like image 156
thefourtheye Avatar answered Sep 22 '22 00:09

thefourtheye