javascript中链接逻辑运算符的简写?

发布于 2024-09-03 01:17:53 字数 229 浏览 3 评论 0原文

有没有更好的方法在 JavaScript 中编写以下条件?

if ( value == 1 || value == 16 || value == -500 || value == 42.42 || value == 'something' ) {
  // blah blah blah
}

我讨厌将所有这些逻辑“或”串在一起。我想知道是否有某种简写。

谢谢!

Is there a better way to write the following conditional in javascript?

if ( value == 1 || value == 16 || value == -500 || value == 42.42 || value == 'something' ) {
  // blah blah blah
}

I hate having all of those logical ORs strung together. I'm wondering if there is some kind of shorthand.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

段念尘 2024-09-10 01:17:53
var a = [1, 16, -500, 42.42, 'something'];
var value = 42;
if (a.indexOf(value) > -1){
// blah blah blah
}

更新:
评论中建议的实用函数示例:

Object.prototype.in = function(){
  for(var i = 0; i < arguments.length; i++){
    if (this == arguments[i]) return true;
  }
  return false;
}

所以你可以写:

if (value.in(1, 16, -500, 42.42, 'something')){
// blah blah blah
}
var a = [1, 16, -500, 42.42, 'something'];
var value = 42;
if (a.indexOf(value) > -1){
// blah blah blah
}

Upd:
Utility function sample as proposed in comments:

Object.prototype.in = function(){
  for(var i = 0; i < arguments.length; i++){
    if (this == arguments[i]) return true;
  }
  return false;
}

So you can write:

if (value.in(1, 16, -500, 42.42, 'something')){
// blah blah blah
}
君勿笑 2024-09-10 01:17:53

您可以扩展数组对象:

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

然后,如果您将所有这些值存储在数组中,您可以执行类似 MyValues.contains(value) 的操作

You could extend the array object:

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

Then if you store all those values in an array you could do something like MyValues.contains(value)

眉黛浅 2024-09-10 01:17:53

不,这是简写。

作为替代方案,您可以执行 switch

switch (value) {
case 1 :
case 16 :
case -500 :
    ....
}

如果您需要很多可能的值,这更容易管理,但实际上您的版本无论如何都较短:)

nope, that is the shorthand.

as an alternative, you can do a switch

switch (value) {
case 1 :
case 16 :
case -500 :
    ....
}

which is easier to manage if you need a lot of possible values, but actually your version is shorter anyway :)

再浓的妆也掩不了殇 2024-09-10 01:17:53
var value= -55;
switch(value){
    case 1: case 16: case -55: case 42.5: case 'something': 
        alert(value); break;        

}
var value= -55;
switch(value){
    case 1: case 16: case -55: case 42.5: case 'something': 
        alert(value); break;        

}
幽蝶幻影 2024-09-10 01:17:53

开关是一个可以接受的选择。您还可以使用地图,具体取决于问题的复杂性(假设您的问题比示例中输入的复杂程度多)。

var accept = { 1: true, 16: true, '-500': true, 42.42: true, something: true };
if (accept[value]) {
  // blah blah blah
}

当然,accept 可以从数组中以编程方式生成。实际上取决于您计划使用此模式的程度。 :/

switch is an acceptable choice. You can also use a map, depending on the complexity of the problem (assuming you have more than you put in your example).

var accept = { 1: true, 16: true, '-500': true, 42.42: true, something: true };
if (accept[value]) {
  // blah blah blah
}

accept could be generated progamatically from an array of course. Depends really on how much you plan on using this pattern. :/

酸甜透明夹心 2024-09-10 01:17:53

好吧,你可以使用 switch 语句...

switch (value) {
  case 1    : // blah
              break;
  case 16   : // blah
              break;
  case -500 : // blah
              break;
  case 42.42: // blah
              break;
  case "something" : // blah
                     break;
}

如果你使用 JavaScript 1.6 或更高版本,你可以在数组上使用 indexOf 表示法:

if ([1, 16, -500, 42.42, "something"].indexOf(value) !== -1) {
   // blah
}

并且为了实现最终的 hackiness,你可以将值强制转换为字符串(这适用于所有浏览器):

if ("1,16,-500,42.42,something".indexOf(value) !== -1) {
   // blah
}

Well, you could use a switch statement...

switch (value) {
  case 1    : // blah
              break;
  case 16   : // blah
              break;
  case -500 : // blah
              break;
  case 42.42: // blah
              break;
  case "something" : // blah
                     break;
}

If you're using JavaScript 1.6 or greater, you can use the indexOf notation on an array:

if ([1, 16, -500, 42.42, "something"].indexOf(value) !== -1) {
   // blah
}

And for the ultimate in hackiness, you can coerce the values to strings (this works for all browsers):

if ("1,16,-500,42.42,something".indexOf(value) !== -1) {
   // blah
}
云雾 2024-09-10 01:17:53

努力尝试另一种方法......

if (/^(1|16|-500|42.42|something)$/.test(value)) {
  // blah blah blah
}

不需要扩展数组原型或任何东西,只需使用快速正则表达式来测试值!

In an effort to make yet another way of doing it...

if (/^(1|16|-500|42.42|something)$/.test(value)) {
  // blah blah blah
}

No need to extend array prototypes or anything, just use a quick regexp to test the value!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文