![](/img/trans.png)
[英]Flow type refinement doesn't work if check null/undefined through another vairable
[英]jQuery check if not null/undefined/empty with != doesn't work
我有这个jQuery代码
if (date != !date) {
console.log(date);
}
date
是一个数组,或者为null
。 如果它是一个数组,我要记录它,如果它为null
我想就此停止它。 我认为!= !var
正是用于此目的。 仍然,当我尝试这样做时,我也会得到null
控制台日志。 怎么会?
x始终不等于!x(这就是x!= !x
含义)。
您想要类似:x是否存在? 它为空吗?
if (date != null) {
console.log(date);
}
var x1; var x2 = [1,2]; if(x1 != null) // <- false console.log(x1); if(x2 != null) // <- true console.log(x2);
试试这个,它应该抓住其他一切...
if(Array.isArray(date)){
console.log(date);
}
else {
console.log('not array');
}
尝试这个:
if (date){
console.log(date);
}
因此,您需要找出某个值是否为数组。 这是ECMAScript标准建议的另一种方法。 有关此的更多信息,请参见这篇文章: 检查对象是否为数组?
var date = ['one', 'two', 'three']; var txt = "bla ... bla ..."; if( Object.prototype.toString.call( date ) === '[object Array]' ) { console.log('is array'); } else { console.log(' not an array'); } if( Object.prototype.toString.call( txt ) === '[object Array]' ) { console.log('is array'); } else { console.log('is not an array'); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.