如何检查 JavaScript 数组中的空字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3457807/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I check JavaScript arrays for empty strings?
提问by Santanu
I need to check if array contains at least one empty elements. If any of the one element is empty then it will return false.
我需要检查数组是否至少包含一个空元素。如果一个元素中的任何一个为空,那么它将返回 false。
Example:
例子:
var my_arr = new Array();
my_arr[0] = "";
my_arr[1] = " hi ";
my_arr[2] = "";
The 0th and 2nd array elements are "empty".
第 0 个和第 2 个数组元素为“空”。
回答by Nick Craver
You can check by looping through the array with a simple for, like this:
您可以通过使用简单的循环遍历数组来检查for,如下所示:
function NoneEmpty(arr) {
for(var i=0; i<arr.length; i++) {
if(arr[i] === "") return false;
}
return true;
}
You can give it a try here, the reason we're not using .indexOf()here is lack of support in IE, otherwise it'd be even simpler like this:
你可以在这里试一试,我们不在.indexOf()这里使用的原因是缺乏 IE 的支持,否则它会更简单像这样:
function NoneEmpty(arr) {
return arr.indexOf("") === -1;
}
But alas, IE doesn't support this function on arrays, at least not yet.
但遗憾的是,IE 不支持数组上的此功能,至少目前不支持。
回答by Azhar
You have to check in through loop.
您必须通过循环签入。
function checkArray(my_arr){
for(var i=0;i<my_arr.length;i++){
if(my_arr[i] === "")
return false;
}
return true;
}
回答by Vitalii Fedorenko
回答by fredrik
You could do a simple help method for this:
你可以为此做一个简单的帮助方法:
function hasEmptyValues(ary) {
var l = ary.length,
i = 0;
for (i = 0; i < l; i += 1) {
if (!ary[i]) {
return false;
}
}
return true;
}
//check for empty
var isEmpty = hasEmptyValues(myArray);
EDIT: This checks for false, undefined, NaN, null, ""and 0.
编辑:这会检查false, undefined, NaN, null,""和0。
EDIT2: Misread the true/false expectation.
EDIT2:误读了真/假期望。
..fredrik
..弗雷德里克
回答by serious
function containsEmpty(a) {
return [].concat(a).sort().reverse().pop() === "";
}
alert(containsEmpty(['1','','qwerty','100'])); // true
alert(containsEmpty(['1','2','qwerty','100'])); // false
回答by pymendoza
Just do a len(my_arr[i]) == 0; inside a loop to check if string is empty or not.
只需len(my_arr[i]) == 0; 在循环内部执行一个循环来检查字符串是否为空。
回答by Johannes Jensen
I see in your comments beneath the question that the code example you give is PHP, so I was wondering if you were actually going for the PHP one? In PHP it would be:
我在问题下方的评论中看到,您给出的代码示例是 PHP,所以我想知道您是否真的要使用 PHP 示例?在 PHP 中,它将是:
function hasEmpty($array)
{
foreach($array as $bit)
{
if(empty($bit)) return true;
}
return false;
}
Otherwise if you actually did need JavaScript, I refer to Nick Craver's answer
否则,如果您确实需要 JavaScript,我会参考 Nick Craver 的回答
回答by Dustin Michels
Using a "higher order function"like filterinstead of looping can sometimes make for faster, safer, and more readable code. Here, you could filter the array to remove items that are notthe empty string, then check the length of the resultant array.
使用“高阶函数”一样filter,而不是循环有时会令更快,更安全,更可读的代码。在这里,您可以过滤数组以删除不是空字符串的项目,然后检查结果数组的长度。
Basic JavaScript
基本 JavaScript
var my_arr = ["", "hi", ""]
// only keep items that are the empty string
new_arr = my_arr.filter(function(item) {
return item === ""
})
// if filtered array is not empty, there are empty strings
console.log(new_arr);
console.log(new_arr.length === 0);
Modern Javascript: One-liner
现代 Javascript:单行
var my_arr = ["", "hi", ""]
var result = my_arr.filter(item => item === "").length === 0
console.log(result);
A note about performance
关于性能的说明
Looping is likely faster in this case, since you can stop looping as soon as you find an empty string. I might still choose to use filterfor code succinctness and readability, but either strategy is defensible.
在这种情况下,循环可能会更快,因为您可以在找到空字符串后立即停止循环。filter为了代码的简洁性和可读性,我可能仍会选择使用,但任何一种策略都是有道理的。
If you needed to loop over all the elements in the array, however-- perhaps to check if everyitem is the empty string-- filter would likely be much faster than a for loop!
但是,如果您需要遍历数组中的所有元素——也许是为了检查每个项目是否为空字符串——过滤器可能比 for 循环快得多!
回答by WasiF
One line solution to check if string have empty element
检查字符串是否有空元素的一行解决方案
let emptyStrings = strArray.filter(str => str.trim().length <= 0);
let strArray = ['str1', '', 'str2', ' ', 'str3', ' ']
let emptyStrings = strArray.filter(str => str.trim().length <= 0);
console.log(emptyStrings)
One line solution to get non-empty strings from an array
从数组中获取非空字符串的单行解决方案
let nonEmptyStrings = strArray.filter(str => str.trim().length > 0);
let strArray = ['str1', '', 'str2', ' ', 'str3', ' ']
let nonEmptyStrings = strArray.filter(str => str.trim().length > 0);
console.log(nonEmptyStrings)
回答by Vishal
For Single dimensional Array
对于一维数组
array.some(Boolean)
For Multi dimensional Array
对于多维数组
array.some(row => row.some(Boolean))

