Javascript 以有效的方式检查多个变量是否不为空、未定义或为空
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32625513/
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
Checking if multiple variables is not null, undefined or empty in an efficient way
提问by Ilja
At the moment I have an if condition like this:
目前我有一个这样的 if 条件:
if(
(variable != null && variable != '' && !variable) &&
(variable2 != null && variable2 != '' && !variable2) &&
(variable3 != null && variable3 != '' && !variable3)
//etc..
)
I need to use it to check if multiple variables have value (were not left out), but I feel like this is a messy solution and wanted to ask if there is a more efficient way? Maybe additional checks as well?
我需要用它来检查多个变量是否有值(没有被遗漏),但我觉得这是一个凌乱的解决方案,想问问是否有更有效的方法?也许还有额外的检查?
回答by Mritunjay
Because if(variable)
ignores any falsy
value, this will work for you
因为if(variable)
忽略任何falsy
值,这对你有用
if(variable && variable2 && variable3)
The following values are always falsy in JS:
以下值在 JS 中始终为假:
false.
0 (zero)
"" (empty string)
null.
undefined.
NaN (a special Number value meaning Not-a-Number!)
Update:-
更新:-
If there is a case when you want to execute if block even if the value is 0, you have to add an extra check saying either 0 or some other value.
如果存在即使值为 0 也想执行 if 块的情况,则必须添加一个额外的检查,说明 0 或其他值。
if((variable || variable === 0) && (variable2 || variable2 === 0) && (variable3 || variable3 === 0))
回答by alex
If your variables are containing some values that are truthy, like a string, and that is considered positive in your condition, then you could simply check using Array.prototype.every()
...
如果您的变量包含一些true值,例如字符串,并且在您的条件下被认为是正值,那么您可以简单地使用Array.prototype.every()
...
if (![var1, var2, var3].every(Boolean)) {
throw new exc;
}
Which will check to ensure every variable has a truthy value.
这将检查以确保每个变量都有一个真值。
回答by realappie
If IE support matter to you (IE9+), you could use the following solution.
如果 IE 支持对您很重要(IE9+),您可以使用以下解决方案。
You could use Array.prototype.some()
. In a nutshell this array method will return true if any of the elements in the array meet a certain condition. This condition will be el == null
this will work because undefined == null
and null == null
both resolve in true
你可以使用Array.prototype.some()
. 简而言之,如果数组中的任何元素满足特定条件,则此数组方法将返回 true。这种情况将是el == null
这会工作,因为undefined == null
和null == null
两决心true
See this stackoverflow post for more information
有关更多信息,请参阅此 stackoverflow 帖子
Which equals operator (== vs ===) should be used in JavaScript comparisons?
在 JavaScript 比较中应该使用哪个等于运算符 (== vs ===)?
Array.prototype.some()
browser support (93% time of writing) here
Array.prototype.some()
浏览器支持(93% 的写作时间)在这里
var a = 5;
var b = null;
var c = undefined;
if ( [a,b,c].some(el => el == null) ) {
console.log("You f*d up")
}
Or you could use Array.prototype.includes()
或者你可以使用 Array.prototype.includes()
See support (93% time of writing) here here
在此处查看支持(93% 的写作时间)
var a = 5;
var b = null;
var c = undefined;
if ( [a,b,c].includes(undefined) || [a,b,c].includes(null) ) {
console.log("You f*d up")
}
If you want good browser support and you don't mind an external library, use lodash.
如果您想要良好的浏览器支持并且不介意外部库,请使用lodash。
var a = 5;
var b = null;
var c = undefined;
if ( _.some([a,b,c], el => _.isNil(el)) ) {
console.log("You f*d up")
}
<script src="https://cdn.jsdelivr.net/lodash/4.17.4/lodash.min.js"></script>
回答by Lee
I assume you are checking for truthy values? If so, you can just do:
我假设您正在检查真实值?如果是这样,你可以这样做:
variable && variable2 && variable3
variable && variable2 && variable3
回答by SiCK
See JavaScript: What is the difference between if (!x)
and if (x == null)
?
请参阅JavaScript:if (!x)
和之间有什么区别if (x == null)
?
!variable will be true for all falsy values so you only have to
!variable 对所有虚假值都为真,所以你只需要
if(variable1 && variable2 && variable3 && ...)
回答by Ramesh Narasappa
Long hand -
长手——
if (var1 !== null || var1 !== undefined || var1 !== '') {
let var2 = var1;
}
Short hand -
简写——
const var2 = var1 || '';