如何正确纠正这个语法:
if (tipoTropaPrioritaria[m] || troopsCount[m] || availableTroops[m]) == ("null" || "undefined") {
...
}
(检查前 3 个变量中是否有任何一个为空或未定义)
最佳答案
您可以定义一个小的辅助函数来进行检查,然后对所有值使用它:
function notset(v) {
return (v === undefined) || (v === null);
}
if (notset(tipoTropaPrioritaria[m]) || notset(troopsCount[m]) ||
notset(availableTroops[m])) {
...
}
关于Javascript ||,如何比较多个变量的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2900333/