Created
January 20, 2017 18:27
-
-
Save solominh/ca9954ee51510434e9e8b560eb5cfe2d to your computer and use it in GitHub Desktop.
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
let a = [1, 2, 3, 4, 5] | |
for (let v of a) { | |
if (v == 3) { | |
a.splice(0, 2); // remove 2 element from index 0 | |
a = undefined; // for..of will have arrayname =a => undefined assignment will not affect for..of | |
} | |
console.log(v); | |
} | |
console.log(a) // undefined | |
// Result: | |
// 1 | |
// 2 | |
// 3 | |
// undefined | |
// => index will always increase and for..of stop when index >= array.length | |
// => when remove 2 elements from array when index = 2 (v =3) => next index(=3) >= array.length(=3) | |
// => remove array elements in for..of is not safe |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment