javascript - Removing Element From Array of Arrays with Regex -


i'm using regex test elements in array of arrays. if inner array doesn't follow desired format, i'd remove main/outer array. regex i'm using working correctly. not sure why isn't removing - can advise or offer edits resolve problem?

for (var = arr.length-1; i>0; i--) {        var = /^\w+$/;       var b = /^\w+$/;       var c = /^\w+$/;        var first = a.test(arr[i][0]);     var second = b.test(arr[i][1]);     var third = c.test(arr[i][2]);  if ((!first) || (!second) || (!third)){ arr.splice(i,1); } 

when cast splice method on array, length updated immediately. thus, in future iterations, jump on of members.

for example:

var arr = ['a','b','c','d','e','f','g']  for(var = 0; < arr.length; i++) {   console.log(i, arr)   if(i%2 === 0) {     arr.splice(i, 1) // remove elements index   } }  console.log(arr) 

it output:

0 ["a", "b", "c", "d", "e", "f", "g"] 1 ["b", "c", "d", "e", "f", "g"] 2 ["b", "c", "d", "e", "f", "g"] 3 ["b", "c", "e", "f", "g"] 4 ["b", "c", "e", "f", "g"]  ["b", "c", "e", "f"] 

my suggestion is, not modify array if still have iterate through it. use variable save it.

var arr = ['a','b','c','d','e','f','g'] var = []  for(var = 0; < arr.length; i++) {   if(i%2) {     another.push(arr[i]) // store elements odd index   } }  console.log(another) // ["b", "d", "f"] 

or go array.prototype.filter, simpler:

arr.filter(function(el, i) {   return i%2 // store elements odd index }) 

it outputs:

["b", "d", "f"]