jQuery 检查是否 null/undefined/empty 与 != 不工作
jQuery check if not null/undefined/empty with != doesn't work
我有这个jQuery代码
if (date != !date) {
console.log(date);
}
date
是一个数组,或者null
。如果它是一个数组,我想记录它,如果它是 null
我想就此停止。我认为 != !var
正是为了这个目的。尽管如此,当我尝试这个时,我也会得到 null
控制台日志。怎么来的?
试试这个:
if (date){
console.log(date);
}
试试这个,它应该捕获 else 中的所有内容...
if(Array.isArray(date)){
console.log(date);
}
else {
console.log('not array');
}
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);
因此您需要确定某个值是否为数组。这是 ECMAScript 标准推荐的另一种方法。有关此的更多信息,请参阅此 post:Check if object is array?
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');
}
我有这个jQuery代码
if (date != !date) {
console.log(date);
}
date
是一个数组,或者null
。如果它是一个数组,我想记录它,如果它是 null
我想就此停止。我认为 != !var
正是为了这个目的。尽管如此,当我尝试这个时,我也会得到 null
控制台日志。怎么来的?
试试这个:
if (date){
console.log(date);
}
试试这个,它应该捕获 else 中的所有内容...
if(Array.isArray(date)){
console.log(date);
}
else {
console.log('not array');
}
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);
因此您需要确定某个值是否为数组。这是 ECMAScript 标准推荐的另一种方法。有关此的更多信息,请参阅此 post:Check if object is array?
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');
}