Last active
March 28, 2017 10:56
-
-
Save webolizzer/bca53f2ac3535b8a16b74ad299d1d353 to your computer and use it in GitHub Desktop.
How to iterate over an array and remove elements in JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var elements = [1, 5, 5, 3, 5, 2, 4]; | |
for(var i = 0; i < elements.length; i++) { | |
if (elements[i] == 5) { | |
// delete item from array & decrement i | |
elements.splice(i--, 1); | |
} | |
} | |
console.log(elements); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment