repl.it
@talha131/

Iterate Reverse 2: Iterate Over An Array And Remove Element

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

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

console.log(eg)
node v10.16.0