How to remove specific array elements from foreach loop in javascript

var fruit = ["apple","pear","pear","pear","banana"]; 

How to remove all "pear" fruits from this array?
I tried the following, but one pear still remains:

 for(var f in fruit) { if ( fruit[f] == "pear" ) { fruit.splice(f, 1); } } for(var f in fruit) { document.write(fruit[f]+"<br>"); } 

Outputs:

 apple pear banana 

What am I doing wrong? Demo version: http://jsfiddle.net/SbxHc/

+8
javascript arrays
source share
10 answers
 var fruit = ["apple", "pear", "pear", "pear", "banana"], i; for (i = 0; i < fruit.length; ++i) { if (fruit[i] === "pear") { fruit.splice(i--, 1); } } console.log(fruit); //["apple", "banana"] 
+25
source share
 for (var i = fruit.length - 1; i > -1; i--) { if (fruit[i] == "pear") fruit.splice(i, 1); } 
+7
source share

When you delete elements from an array when it moves through it, you usually repeat backward so that the current index does not skip elements when they are deleted:

 var fruit = ["apple","pear","pear","pear","banana"]; var i; for (i = fruit .length - 1; i >= 0; i--) { if (fruit [i] === "pear") { fruit .splice(i, 1); } } console.log(fruit ); 
+3
source share

If there are a lot of β€œwrong” elements in the original array, I suggest at least not using the in-place array, but collecting all the β€œcorrect” elements into a new array:

 var rightFruits = []; for (var i = 0, len = fruit.length; i < len; ++i) { if (fruit[i] !== 'pear') { rightFruits.push(fruit[i]); } } 
+2
source share
 var i; for (i = 0; i < fruits.length; i += 1) { if (fruits[i] == "pear") { fruits.splice(i, 1); i -= 1; } } 

6.4. Listing

Because JavaScript arrays are really objects, the for in operator can be used to iterate over all the properties of the array. Unfortunately, for in makes no guarantees about the order of properties, and most arrays expect elements to be created in numerical order. In addition, there is still a problem with unexpected properties being unloaded from the prototype circuit.

Fortunately, the usual approval problem resolves these issues. JavaScript for the operator is similar to that in most C-like languages. It is controlled by three sentences: the first initializes the loop, the second is the while condition, and the third is incremented:

 var i; for (i = 0; i < myArray.length; i += 1) { document.writeln(myArray[i]); } 
+1
source share
 var i; while ((i = fruit.indexOf("pear")) > -1) fruit.splice(i,1); 

Remember that Array.indexOf is not supported by IE8 and below. -_-

0
source share

Why not iterate over the list? Thus, when you remove an item from the list and change the length, it does not violate the loop logic:

 var fruit = ["apple","pear","pear","pear","banana"]; for (var i = fruit.length - 1; i >= 0; i--) { if (fruit[i] === "pear") { fruit.splice(i, 1); } } console.log(fruit); // ["apple", "banana"] 
0
source share

If he doesn't want to repeat the iteration, you can use a combination of while and pop-function of the array

 var daltons = [ { id: 1, name: "Joe" }, { id: 2, name: "William" }, { id: 3, name: "Jack" }, { id: 4, name: "Averell" } ]; var dalton; while (dalton=daltons.pop()) { console.log(dalton.name); } console.log(daltons); 
0
source share

You can also use filter :

 let noPears = fruits.filter(fruit => fruit != 'pear') 
0
source share
 for(var f in fruit) { if ( fruit[f] == "pear" ) { fruit.splice(f-1, 1); } } for(var f in fruit) { document.write(fruit[f]+"<br>"); } 

to use

-one
source share

All Articles