javascript 与多个值进行比较的简洁方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13737091/
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
Concise way to compare against multiple values
提问by FatDogMark
Consider the following:
考虑以下:
var a = 'jesus';
if(a == 'something' || a == 'nothing' || a=='anything' || a=='everything'){
alert('Who cares?');
}
Is there a way to make this shorter?
有没有办法缩短这个时间?
Is there anything in Javascript like if (a=='bbb'||'ccc')
?
Javascript 中有什么东西if (a=='bbb'||'ccc')
吗?
In addition, can jQuery help here?
另外,jQuery 能帮上忙吗?
采纳答案by Christophe
With a regex:
使用正则表达式:
if (/^(something|nothing|anything|everything)$/.exec('jesus')) alert('Who cares?');?
Or the opposite:
或者相反:
/^(something|nothing|anything|everything)$/.exec('jesus')||alert('Who cares?');?
[Update] Even shorter ;-)
[更新] 更短;-)
if (/^(some|no|any|every)thing$/.exec('jesus')) alert('Who cares?');?
回答by alex
You could use this...
你可以用这个...
if (["something", "nothing", "anything", "everything"].includes(a)) {
alert('Who cares?');
}
If you're stuck with older browser support...
如果您坚持使用较旧的浏览器支持...
if (["something", "nothing", "anything", "everything"].indexOf(a) > -1) {
alert('Who cares?');
}
You also tagged it jQuery, so if you need to support older browsers without Array.prototype.indexOf()
, you could use $.inArray()
.
您还将它标记为 jQuery,因此如果您需要支持不带 的旧浏览器Array.prototype.indexOf()
,则可以使用$.inArray()
.
回答by Adil
You can put the options in array and use jQuery $.inArray()
or javascrpt indexOf()
to search array
您可以将选项放在数组中并使用 jQuery$.inArray()
或 javascrptindexOf()
来搜索数组
Pure javascript?
纯javascript?
var a = 'anything';
arr = ['something', 'nothing', 'anything', 'everything'];
if(arr.indexOf(a) != -1)
alert("condition met");
else
alert("condition not met");
With jQuery
使用 jQuery
var a = 'jesus';
arr = ['something', 'nothing', 'anything', 'everything'];
if($.inArray(a, arr) != -1) // With jQuery
alert("condition met");
else
alert("condition not met");
回答by Maciej Bukowski
With ES7 you are able to use Array.prototype.includesand even shorter notation than indexOf, which is already implemented in every modern browser.
使用 ES7,您可以使用Array.prototype.includes甚至比 indexOf 更短的符号,它已经在每个现代浏览器中实现。
Here is the example usage:
这是示例用法:
if (['hero', 'anything', 'everything'].includes(me)) {
alert('Who cares?');
}
And the polyfillfrom Mozilla.
还有来自 Mozilla的polyfill。
回答by suresh.g
Try this:
试试这个:
If you want to check the words other than Jesus, try following,
如果您想检查耶稣以外的词,请尝试以下,
if(a != "jesus"){
alert('Who cares?');
}
if you want to check particular words, try following,
如果您想检查特定单词,请尝试以下操作,
var check_arrays = ['something','nothing', 'anything', 'everything'];
if(checkThis(a)){
alert('Who cares?');
}
function checkThis(a)
{
for(i=0;i<check_arrays.length;i++)
{
if(check_arrays[i] == a)
return true;
}
return false;
}
回答by Akhil Sekharan
May be using switch instead of if:
可能正在使用 switch 而不是 if:
var a = 'jesus';
switch (a){
case 'something':
case 'nothing' :
case 'anything':
case 'everything':
alert('Who cares?');
break;
}
回答by Christophe
Using an object literal:
使用对象字面量:
var all={something:1,nothing:1,anything:1,everything:1};
if (all.hasOwnProperty('jesus')) alert('Who cares?');?
I'd say this is the most concise cross-browser expression for general use (well, as long as the objective is to compare strings). It is also very flexible as you can easily add or remove properties:
我会说这是一般用途的最简洁的跨浏览器表达式(好吧,只要目标是比较字符串)。它也非常灵活,因为您可以轻松添加或删除属性:
all.mything=1;
delete all.nothing;
回答by Miqdad Ali
I think there is no easy method to do this. But you can write a function in_array
and you can use it anywhere you need
我认为没有简单的方法可以做到这一点。但是你可以写一个函数in_array
,你可以在任何你需要的地方使用它
Array.prototype.in_array = function(p_val) {
for(var i = 0, l = this.length; i < l; i++) {
if(this[i] == p_val) {
return true;
}
}
return false;
}
var v_array = [ 'something','nothing', 'anything', 'everything'];
var a = "jesus";
if(v_array.in_array(a)){
alert('Who cares?');
}