检查空字符串数组 [英] Checking for an empty string array
本文介绍了检查空字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试检查像这样的空字符串数组:
I'm trying to check for an empty string array like this:
var array = ['',''];
//Other code
if (array == ['',''])
//Do stuff
,我发现
['',''] == ['','']
返回 false
。这是为什么?我应该使用哪种方法检查空字符串数组?
returns false
. Why is that? What method should I use to check for an empty string array? Is there a way where I don't have to check each individual item?
推荐答案
除了提议使用的 Array#toString
方法,我建议使用 Array#join
,以空字符串作为分隔符,然后测试结果。优点是,它适用于数组内部任意数量的元素。
Beside the proposed using of Array#toString
method, I suggest to use Array#join
with an empty string as separator and then test the result. The advantage is, it works for an arbitrary count of elements inside of the array.
var bool = array.join('') ? 'not all empty string' : 'all empty string';
这篇关于检查空字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文