简体   繁体   English

在JavaScript中从数组中删除空字符串

[英]Removing empty strings from array in javascript

I need help understanding whats going on in my code. 我需要帮助来了解我的代码中发生了什么。 I plan on writing a function that takes in a string and removes all letters. 我打算编写一个接受字符串并删除所有字母的函数。 The input will be a combination of letters and numbers. 输入将是字母和数字的组合。 I want this function to return an array of all the numbers found in the string. 我希望此函数返回在字符串中找到的所有数字组成的数组。 I have successfully written something(with the help of stackoverflow): 我已经成功地写了一些东西(借助stackoverflow):

number = "32321FDFDS 44"
arr = number.replace(/[A-Za-z]/g," ").split(" ")



for(var i = arr.length - 1; i >= 0; i--){
     if(arr[i] == "") {
          arr.splice(i, 1);
    }
}

This returns 这返回

[ '32321', '44' ]

Which is perfect for now. 目前最适合。 My question is I don't understand how arr.splice(i,1) is removing empty strings. 我的问题是我不明白arr.splice(i,1)如何删除空字符串。 It doesn't make sense that it is removing the empty strings in arr . 删除arr的空字符串是没有意义的。 Can someone help me understand this? 有人可以帮我理解吗?

Test : 测试:

if (arr[n] == "") // if item `n` within `arr` array `==` `""` , do stuff

See Array.prototype.splice() 参见Array.prototype.splice()

With two items within an array : 数组中有两个项目:

 var arr = ["123", ""]; if (arr[0] == "") { arr.splice(0,1); } else { console.log(arr); // ["123", ""]; }; if (arr[1] == "") { arr.splice(1,1); console.log(arr); // ["123"] }; 

Unlike other methods that return a new array, leaving the original variable alone, the .splice method mutates an array, making in-place changes to it. 与其他方法返回一个新数组而.splice保留原始变量的其他方法不同, .splice方法对数组进行突变,对其进行原位更改。

The statement arr.splice(i, 1); 语句arr.splice(i, 1); means starting at position i remove one item from the array arr . 表示从位置i开始,从数组arr删除一项。 if(arr[i] == "") means if the item at position i is and empty string, do the stuff inside this block. if(arr[i] == "")表示如果位置i处的项目为空字符串,则在此块内执行操作。 So when the if statement is true that item is removed from the array. 因此, if语句为true,则将该项目从数组中删除。

That said unless you need to support ES3 browsers (which effectively means IE8 or below), instead of looping through the array like that, I would just use the .filter method : 也就是说,除非您需要支持ES3浏览器(实际上意味着IE8或更低版本),否则我将只使用.filter方法 ,而不是像这样遍历整个数组:

var number = "32321FDFDS 44",
  arr = number.replace(/[A-Za-z]/g," ").split(" ").filter(function (item) {
    return !!item; // all strings except an empty string will coerce to true
  });

console.log(arr);

jsFiddle 的jsfiddle

If you are just trying to extract an array of numeric strings from a string with no other requirements, an even more succinct way of doing it would be to just split on one or more non-numbers: 如果您只是试图从没有其他要求的字符串中提取数字字符串数组,那么更简洁的方法是将一个或多个非数字拆分:

var number = "32321FDFDS 44",
  arr = number.split(/\D+/);

// arr is now [ "32321", "44" ]
console.log(arr);

This does it all in one step without having to filter out empty strings at all. 这一步完成了所有操作,而根本不需要过滤掉空字符串。

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

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