Skip to content

Instantly share code, notes, and snippets.

@webolizzer
Last active March 28, 2017 10:56
Show Gist options
  • Save webolizzer/bca53f2ac3535b8a16b74ad299d1d353 to your computer and use it in GitHub Desktop.
Save webolizzer/bca53f2ac3535b8a16b74ad299d1d353 to your computer and use it in GitHub Desktop.
How to iterate over an array and remove elements in JavaScript
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