javascript 如何将字符串与多个值进行比较?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12782195/
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
how can I compare a string with several values?
提问by Diego
Possible Duplicate:
array.contains(obj) in JavaScript
Something like:
就像是:
if (mystring == "a" || mystring == "b" || mystring =="c")
I was hopping to do:
我想做:
if (mystring in ("a", "b", "c"))
is it possible?
是否可以?
回答by Sirko
You could use indexOf()
like this
你可以使用indexOf()
这样的
if ( [ 'a', 'b', 'c' ].indexOf( mystring ) > -1 ) { ... }
EDITWith ES7 comes a little simplification. As of today just Chrome 47+ and FF 43+ seem to support it:
编辑ES7 带来了一些简化。截至今天,似乎只有 Chrome 47+ 和 FF 43+ 支持它:
if ( [ 'a', 'b', 'c' ].includes( mystring ) ) { ... }
回答by WTK
Using indexOf is first thing that comes to mind, however you have to keep in mind, that there's no .indexOf
function in older IEs(so you would have to use your custom code to simulate it, or just go straight to something like jQuery.inArrayor underscore.js indexOf).
使用 indexOf 是首先想到的事情,但是您必须记住,旧版 IE 中没有.indexOf
函数(因此您必须使用自定义代码来模拟它,或者直接使用jQuery.inArray或underscore.js indexOf)。
if ([ 'a', 'b', 'c' ].indexOf( mystring ) > -1 ) { ... }
Side note: as you can see by looking at inArray definition in jQuery source, writing your own indexOf replacement is pretty easy. So like I said - write your own method, copy-paste it from other libraries or just use those libs if you want to be able to use indexOf in every browser out there.
旁注:通过查看 jQuery 源代码中的 inArray 定义可以看出,编写自己的 indexOf 替换非常容易。所以就像我说的 - 编写自己的方法,从其他库中复制粘贴它,或者如果您希望能够在每个浏览器中使用 indexOf 就使用这些库。
回答by Jacob George
You could do it the old way
你可以用旧的方式来做
a = "a";
b = ["a","b","c","d"];
function check(a,b){
i = 0;
for (i=0;i<b.length;i++) {
if (a === b[i]) {
return true;
}
}
return false;
}
alert(check (a,b))
note that indexOf is a recent addition to the ECMA-262 standard; as such it may not be present in all browsersIf you're going to use this with IE it will only work with version 9 or above
请注意indexOf 是最近添加到 ECMA-262 标准的;因此它可能不会出现在所有浏览器中如果你打算在 IE 中使用它,它只能在版本 9 或更高版本中使用
回答by Martin
I think something like this:
我认为是这样的:
if (["a", "b", "c"].indexOf(mystring) != -1) {
// do something
}
回答by user1366660
if (["a", "b", "c"].indexOf(mystring) != -1) {}
This would be the best way, however it may not work on some browsers.
这将是最好的方法,但它可能不适用于某些浏览器。
回答by Prasanth
You can use indexOf
您可以使用 indexOf
[1,2,3].indexOf(1) will give 0
[1,2,3].indexOf(4) will give -1
So, you can check if indexOf returns -1.
因此,您可以检查 indexOf 是否返回 -1。
Similar for strings.
类似于字符串。
回答by Denys Séguret
Your version is the fastest one for three chars, and the most readable.
您的版本是三个字符最快的版本,也是最易读的版本。
With a little more possible values, you could use something like this :
使用更多可能的值,您可以使用以下内容:
var c;
if (s.length==1 && (c=s.charAt(0))>='a' && c<='c') {
But that's mainly for fun...
但这主要是为了好玩……
回答by KooiInc
Addtionally: you could create a String extension for the comparison:
另外:您可以为比较创建一个字符串扩展:
String.prototype.in = function(){
return ~[].slice.call(arguments).indexOf(this.valueOf()) ? true : false;
}
//usage
var myStr = 'a';
myStr.in('a','b','c'); //=> true
'd'.in('a','b','c'); //=> false
For older browsers: this MDN pagecontains a shim for Array.indexOf
对于较旧的浏览器:此MDN 页面包含一个 shimArray.indexOf
回答by alexclooze
Something like
就像是
foreach (str in strings) {
if (mystring.contains(str)) {
...
}
}
may work.
可能工作。