repl.it
@talha131/

Decrement i: Iterate Over An Array And Remove Elements-1

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
14
const eg = [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610];

for (let i = 0; i < eg.length; 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}`)
    // Important
    i--;
  }
}

console.log(eg)
node v10.16.0