【问题标题】:Javascript ||, how to compare multiple variables value?Javascript ||,如何比较多个变量的值?
【发布时间】:2010-05-24 21:07:55
【问题描述】:

如何正确纠正这种语法:

if (tipoTropaPrioritaria[m] || troopsCount[m] || availableTroops[m]) == ("null" || "undefined") {

...

}

(检查前 3 个变量是否为空或未定义)

【问题讨论】:

  • 您是指nullundefined 的值还是"null""undefined" 的字符串值?
  • 不是字符串值,“内部 javascript”空/未定义值。
  • 满意后记得接受解决方案。

标签: javascript operators comparator


【解决方案1】:

您可以定义一个小的辅助函数来进行检查,然后在所有值上使用它:

function notset(v) {
   return (v === undefined) || (v === null);
}

if (notset(tipoTropaPrioritaria[m]) || notset(troopsCount[m]) ||
    notset(availableTroops[m])) {
  ...
}

【讨论】:

  • @Lucas:我想这些应该可以工作,但使用命名函数而不是定义它“内联”可能会更具可读性。
【解决方案2】:

用途:

if (tipoTropaPrioritaria[m] == null || troopsCount[m] == null || availableTroops[m] == null || 
    tipoTropaPrioritaria[m] == undefined || troopsCount[m] == undefined || availableTroops[m] == undefined) {
    // ...
}

编辑:在这种情况下使用===(threequals)运算符可能会更好。

if (tipoTropaPrioritaria[m] === null || troopsCount[m] === null || availableTroops[m] === null || 
    tipoTropaPrioritaria[m] === undefined || troopsCount[m] === undefined || availableTroops[m] === undefined) {
    // ...
}

或:

if (null in {tipoTropaPrioritaria[m]:0, troopsCount[m]:0, availableTroops[m]:0} || undefined in {tipoTropaPrioritaria[m]:0, troopsCount[m]:0, availableTroops[m]:0}) {

【讨论】:

  • 您应该使用typeof foo == "undefined",因为 undefined 可以设置为具有不同值的变量。
  • 什么是typeof foo,为什么要使用它?
  • 我将使用 if ({tipoTropaPrioritaria[m]:0 中的 null,forcesCount[m]:0,availableTroops[m]:0} || 在 {tipoTropaPrioritaria[m] 中未定义: 0, teamsCount[m]:0, availableTroops[m]:0}) 我应该使用 typeof foo 吗??
  • 啊。我担心这个技巧可能不适用于更复杂的表达式。给我几分钟,我会设法找到解决办法。
  • 我认为没有简单的解决方法 - 我会在我的答案或另一个中使用一个更简单的版本。 pastebin.com/8Q5NJCjL 包含一个可能的方法,如果你真的想使用我演示的第二种方法,但它更像是一种学术练习,而不是实际应该使用的东西。
【解决方案3】:

做法是:

if ((tipoTropaPrioritaria[m] == null || tipoTropaPrioritaria[m] == undefined)
|| (troopsCount[m] == null || troopsCount[m] == undefined) ||
(availableTroops[m] == null || availableTroops[m] == undefined)) {
...
}

【讨论】:

    【解决方案4】:

    如果你经常这样做,你可以创建一个辅助函数

    function isNullOrUndef(args) {
        for (var i =0; i < arguments.length; i++) {
            if (arguments[i] == null || typeof arguments[i] === "undefined") {
                return true
            }
        }
        return false
    }
    
    if (isNullOrUndef(tipoTropaPrioritaria[m], troopsCount[m], availableTroops[m]))
      ...
    

    【讨论】:

      【解决方案5】:

      这是做你想做的最好的方式

      if (!tipoTropaPrioritaria[m] || !troopsCount[m] || !availableTroops[m]) {
          ...
      }
      

      ! 运算符将结果强制转换为可测试的布尔值(null 和 undefined 变为 false),并且前面的 ! false 被否定为 true

      另一种方法是针对nullundefined 测试每个表达式。

      function isNullOrUndefined(val) {
          return (val === null || typeof val == "undefined"); 
      }    
      
      if (isNullOrUndefined(a) || isNullOrUndefined(b) ... ) {
      

      所以你知道,测试未定义的正确方法是

      if (typeof foo === "undefined") {...
      

      【讨论】:

      • 我想你的意思是==,而不是=
      • 小心非特异性测试。 false 的非空布尔值将被视为与 null 相同,除非您测试 undefined/null。
      • 唯一的问题是 availableTroops[m] 可能合法地为 0,这可能需要与 null 或 undefined 不同的行为。
      • @John:不,=== 是要走的路!!
      • @Claudio:保证typeof返回一个字符串,所以不需要===
      【解决方案6】:
      if (tipoTropaPrioritaria[m] && troopsCount[m] && availableTroops[m]) { }
      else { /* At least ones is undefined/null OR FALSE */ }
      
      if (tipoTropaPrioritaria[m] == null || troopsCount[m] == null || availableTroops[m] == null)
      { /* One is null. */ }
      

      【讨论】:

      • 如果其中一个是0 在第一个代码sn-p 中会发生什么?
      • @Matt 这就是“OR FALSE”评论的意思(零、假、空)
      【解决方案7】:

      如果你想检查它们是否为空或未定义,你可以写

      if (!tipoTropaPrioritaria[m] || !troopsCount[m] || !availableTroops[m])
      

      nullundefined 都是 falsely 值。那么只有当这些都不是 nullundefined 时才会如此。 请注意还有其他错误值:

      • 0
      • 空字符串
      • NaN

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-01-28
        • 1970-01-01
        • 2019-09-29
        • 1970-01-01
        • 2014-06-08
        相关资源
        最近更新 更多