简体   繁体   English

使用正则表达式从数组数组中删除元素

[英]Removing Element From Array of Arrays with Regex

I'm using regex to test certain elements in an array of arrays. 我正在使用正则表达式来测试数组数组中的某些元素。 If an inner array doesn't follow the desired format, I'd like to remove it from the main/outer array. 如果内部数组未遵循所需的格式,我想将其从主/外部数组中删除。 The regex I'm using is working correctly. 我正在使用的正则表达式工作正常。 I am not sure why it isn't removing - can anyone advise or offer any edits to resolve this problem? 我不确定为什么不删除它-任何人都可以建议或提供任何修改来解决此问题?

for (var i = arr.length-1; i>0; i--) {

      var a = /^\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 you cast splice method on an array, its length is updated immediately. 当您在数组上转换splice方法时,其length会立即更新。 Thus, in future iterations, you will probably jump over some of its members. 因此,在将来的迭代中,您可能会跳过其某些成员。

For example: 例如:

var arr = ['a','b','c','d','e','f','g']

for(var i = 0; i < arr.length; i++) {
  console.log(i, arr)
  if(i%2 === 0) {
    arr.splice(i, 1) // remove elements with even index
  }
}

console.log(arr)

It will 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, do not modify the array itself if you still have to iterate through it. 我的建议是,如果仍然需要遍历数组,请不要修改数组本身。 Use another variable to save it. 使用另一个变量将其保存。

var arr = ['a','b','c','d','e','f','g']
var another = []

for(var i = 0; i < arr.length; i++) {
  if(i%2) {
    another.push(arr[i]) // store elements with odd index
  }
}

console.log(another) // ["b", "d", "f"]

Or you could go with Array.prototype.filter , which is much simpler: 或者,您可以使用Array.prototype.filter ,它要简单得多:

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

It also outputs: 它还输出:

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

Your code seems to work to me. 您的代码似乎对我有用。 The code in your post was missing a } to close the for statement but that should have caused the script to fail to parse and not even run at all. 您帖子中的代码缺少}来关闭for语句,但这应该导致脚本无法解析,甚至根本无法运行。

I do agree with Leo that it would probably be cleaner to rewrite it using Array.prototype.filter though. 我同意Leo的观点,尽管使用Array.prototype.filter重写它可能会更清洁。

The code in your question would look something like this as a filter: 您问题中的代码看起来像是过滤器:

arr = arr.filter(function (row) {
  return /^\w+$/.test(row[0]) && /^\w+$/.test(row[1]) && /^\w+$/.test(row[2]);
});

jsFiddle 的jsfiddle

I'm assuming it is 3 different regular expressions in your actual code, if they are all identical in your code you can save a little overhead by defining the RegExp literal once: 我假设您的实际代码中有3个不同的正则表达式,如果它们在您的代码中都相同,则可以通过一次定义RegExp文字来节省一些开销:

arr = arr.filter(function (row) {
  var rxIsWord = /^\w+$/;
  return rxIsWord.test(row[0]) && rxIsWord.test(row[1]) && rxIsWord.test(row[2]);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM