I am trying to iterate an array with forEach and, based on a condition, I do something.
For this question, I simplified the condition in order to try to understand what's going on.
I expect array b = [] after the operation, but it is not, as it operates only on half on the elements. Why does this happen?
(This is not about removing everything from b, just trying to understand why it jumps the even indexes).
var a = [1, 2, 3, 4, 5, 6];
var b = a.slice(0);
console.log('before b = ', b); // b = [1, 2, 3, 4, 5, 6]
a.forEach(function (e) {
if (e > 0) {
b.splice(a.indexOf(e), 1);
}
});
console.log('after b = ', b); // b = [2, 4, 6]
// but I expect b = []