repl.it
@talha131/

Incorrect forward: Iterate Over An Array And Remove Elements

Nodejs

https://www.oncrashreboot.com/iterate-over-an-array-and-remove-elements

fork
loading
Files
  • index.js
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
const eg = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610];

const len = eg.length

for (let i = 0; i < len; i++) {
  console.log(i, eg[i], eg.length);
  if (eg[i] > 99) {
    // Remove element
    eg.splice(i, 1);
    console.log(`Element at ${i} removed. Length is  ${eg.length}`);
  }
}
              
node v10.16.0