首页 > 解决方案 > Javascript If 语句中的多个条件

问题描述

这是一个令人尴尬的基本问题,但我无法在 MDN、W3schools、here 或 Google 搜索上找到任何我理解的答案。

当引用多个条件时,例如在 if 语句中:

if ((a != b) && (a != c) && (a != d)){...}

(考虑到列表可以有多长,)必须有一种更有效的方法来做到这一点。我想它看起来像这样:

if (a != (b || c || d)){...}

这不起作用,但必须有一些其他有效的解决方案。如果可以,请告诉我。谢谢。

编辑:显然有价值的信息:所有变量都是原语。

标签: javascriptif-statementconditional-statements

解决方案


您可以Array使用所有可能的值创建一个并使用Array.prototype.indexOf()orArray.prototype.includes()来检查一个给定的值是否在里面Array

const values = [1, 2, 3, 4, 5, 7, 8, 9];

console.log(`[${ values.join(', ') }].indexOf(1) = ${ values.indexOf(1) }`);
console.log(`[${ values.join(', ') }].indexOf(6) = ${ values.indexOf(6) }`);
console.log(`[${ values.join(', ') }].includes(1) = ${ values.includes(1) }`);
console.log(`[${ values.join(', ') }].includes(6) = ${ values.includes(6) }`);

如您所见,如果值不在第一次出现的索引或索引中,则indexOf()返回,根据您的用例,这可能很有用,而仅在值存在或其他情况下返回,并且在 IE 中不受支持.-1Arrayincludes()truefalse

所以在你的情况下,你最终会得到这样的结果:

const b = 'b';
const c = 'c';
const d = 'd';

const values = [b, c, d];

let target = 'a';

if (!values.includes(target)){
  console.log(`${ target } not in [${ values.join(', ') }]`);
}

target = 'b';

if (values.includes('b')){
  console.log(`${ target } in [${ values.join(', ') }]`);
}

// You could also do [b, c, d].includes(target) if you don't need to reuse that Array.

这将适用于原始值,因此stringnumberbooleannullundefinedsymbol但也适用于Objectreferences,不要与 just 混淆Object,如下所示:

const values = [{ foo: 1 }, { bar: 2 }, { baz: 3 }];
const target = { foo: 1 };

console.log(`values.includes(target) = ${ values.includes(target) }`);

// This will not work because the { foo: 1 } object inside values and the one we are passing to
// includes are not the same, as you can see here:

console.log(`values[0] === target = ${ values[0] === target }`);

// However, if we get a reference to it, then those two references can be compared successfully:

const reference = values[0];

console.log(`values[0] === reference = ${ values[0] === reference }`);

// So now we can also use includes or indexOf:

console.log(`values.includes(reference) = ${ values.includes(reference) }`);

如果您想了解它是如何工作的,您可以尝试实现类似于indexOforincludes自己的功能。像这样简单的东西:

function isValueInArray(arr, value) {

  const size = arr.length;
  
  // Iterate over all the indexes in arr:
  for (let i = 0; i < size; ++i) {
  
    // Check whether the value at the current index matches the target value:
    if (arr[i] === value) {
    
      // If so, we have found value at index i in arr, so we return true and stop searching:
      return true;
      
      // Note we could also return the index if we care about the position the value is at.
    }
  }
  
  // We have checked all the values in arr and none of them matched, so we return false: 
  return false;
}

const values = [0, 1, 2, 3, 4, 5, 7, 8, 9];

console.log(`isValueInArray(values, 1) = ${ isValueInArray(values, 1) }`);
console.log(`isValueInArray(values, 6) = ${ isValueInArray(values, 6) }`);

但是,请记住,真正的实现稍微复杂一些。例如,你可以看看这个 polyfill forincludes .


推荐阅读