测试 Javascript 数组中的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5252650/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 16:22:32  来源:igfitidea点击:

Test for value in Javascript Array

javascript

提问by Phillip Senn

In SQL Server, I could say:

在 SQL Server 中,我可以说:

WHERE X IN(1,2)

How would you rewrite the following in JavaScript:

你将如何用 JavaScript 重写以下内容:

if (X==1 || X==2) {}

回答by Peter Olson

Use indexOfto see if xis in an array.

使用indexOf,看是否x是在数组中。

if([1,2].indexOf(x) !== -1)

回答by vitkon

Using Array .includesmethod.

使用数组.includes方法

if ([1, 2].includes(x)) {
  // array has x
}

回答by p.campbell

Try using an array, and then its .indexOf().

尝试使用数组,然后使用它的.indexOf().

 var myNumbers = [1,2];
 var foo = 4;
 var bar = 1;

 var exists = (myNumbers.indexOf(bar) > -1); //true
 var notExists = (myNumbers.indexOf(foo) > -1); //false

回答by adamJLev

There's no silver bullet. There will be a few gotchas.

没有银弹。会有一些问题。

If you do indexOfas some answers suggest, you need to remember that Array.indexOfis not supported in all browsers, so you need to provide your own fallback. Also this will have a performance of O(n)since it needs to traverse the whole array, which might not be ideal if you're dealing with a huge array.

如果您indexOf按照某些答案的建议进行操作,则需要记住Array.indexOf并非所有浏览器都支持该功能,因此您需要提供自己的回退。这也将有一个性能,O(n)因为它需要遍历整个数组,如果您正在处理一个巨大的数组,这可能并不理想。

If you use the inoperator as other answers suggest, you need to remember that in Javascript object's properties are always strings, so don't expect ===checks to work if you're checking for numbers.

如果您in按照其他答案的建议使用运算符,则需要记住,在 Javascript 中对象的属性始终是字符串,因此===如果您正在检查数字,请不要指望检查会起作用。

In this particular example you suggested, I would just go for the good old if (X==1 || X==2).

在您建议的这个特定示例中,我会选择旧的if (X==1 || X==2).

回答by Tom Gruner

if (x in {1:true, 2:true}) { }

Or, if you want to abstract it you could do it like this http://snook.ca/archives/javascript/testing_for_a_v

或者,如果你想抽象它,你可以像这样http://snook.ca/archives/javascript/testing_for_a_v

function oc(a) { var o = {}; for(var i=0;i

函数 oc(a) { var o = {}; for(var i=0;i

Still... not the most scalable thing to be doing

仍然......不是最可扩展的事情

回答by vbence

Requires Javascript 1.6

需要 Javascript 1.6

if ((new Array(1, 2)).indexOf(X) != -1) {
}

回答by vbence

I know we have in_array() function in PHP, but I never heard of a similar function in JS. I think you gotta do it the old way:

我知道我们在 PHP 中有 in_array() 函数,但我从未听说过在 JS 中有类似的函数。我认为你必须用旧的方式来做:

function contains(a, obj) {
  var i = a.length;
  while (i--) {
    if (a[i] === obj) {
      return true;
    }
  }
  return false;
}

回答by Piyush Mattoo

Function to convert the array into an object literal

将数组转换为对象字面量的函数

function oc(a)
{
 var o = {};
 for(var i=0;i<a.length;i++)
 {
  o[a[i]]='';
 }
  return o;
}

You can call the function like

你可以像这样调用函数

if( name in oc(['Tom', 'Harry','Sue']) ) { ... }

回答by ypercube??

Just fun:

只是好玩:

if (X*X+1*2 == (1+2)*X) {}

or self-explaining:

或不言自明:

if ((X-1)*(X-2) == 0) {}