Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 是否可以以某种方式缩短switch语句?也许是打个圈?_Javascript_Php_Python_Bash_Switch Statement - Fatal编程技术网

Javascript 是否可以以某种方式缩短switch语句?也许是打个圈?

Javascript 是否可以以某种方式缩短switch语句?也许是打个圈?,javascript,php,python,bash,switch-statement,Javascript,Php,Python,Bash,Switch Statement,独立于哪种编程语言,有哪些选项可以缩短具有许多类似情况的长switch语句 通过搜索,我找到了如下答案,但我的情况都不同,只有一个整数在循环中变化 我经常在bash/PHP/Python/JavaScript中使用以下switch语句构造,我正在寻找一个较短的版本: switch ($device) { // 2ghz case "n2": return 1; break; case "nne2": return 2;

独立于哪种编程语言,有哪些选项可以缩短具有许多类似情况的长switch语句

通过搜索,我找到了如下答案,但我的情况都不同,只有一个整数在循环中变化

我经常在bash/PHP/Python/JavaScript中使用以下switch语句构造,我正在寻找一个较短的版本:

switch ($device) {
    // 2ghz
    case "n2":
        return 1;
        break;
    case "nne2":
        return 2;
        break;
    case "ne2":
        return 3;
        break;
    case "ene2":
        return 4;
        break;
    case "e2":
        return 5;
        break;
    case "ese2":
        return 6;
        break;
    case "se2":
        return 7;
        break;
    case "sse2":
        return 8;
        break;
    case "s2":
        return 9;
        break;
    case "ssw2":
        return 10;
        break;
    case "sw2":
        return 11;
        break;
    case "wsw2":
        return 12;
        break;
    case "w2":
        return 13;
        break;
    case "wnw2":
        return 14;
        break;
    case "nw2":
        return 15;
        break;
    case "nnw2":
        return 16;
        break;

    // 5ghz
    case "n5":
        return 17;
        break;
    case "nne5":
        return 18;
        break;
    case "ne5":
        return 19;
        break;
    case "ene5":
        return 20;
        break;
    case "e5":
        return 21;
        break;
    case "ese5":
        return 22;
        break;
    case "se5":
        return 23;
        break;
    case "sse5":
        return 24;
        break;
    case "s5":
        return 25;
        break;
    case "ssw5":
        return 26;
        break;
    case "sw5":
        return 27;
        break;
    case "wsw5":
        return 28;
        break;
    case "w5":
        return 29;
        break;
    case "wnw5":
        return 30;
        break;
    case "nw5":
        return 31;
        break;
    case "nnw5":
        return 32;
        break;

    // 24ghz
    case "n24":
        return 33;
        break;
    case "nne24":
        return 34;
        break;
    case "ne24":
        return 35;
        break;
    case "ene24":
        return 36;
        break;
    case "e24":
        return 37;
        break;
    case "ese24":
        return 38;
        break;
    case "se24":
        return 39;
        break;
    case "sse24":
        return 40;
        break;
    case "s24":
        return 41;
        break;
    case "ssw24":
        return 42;
        break;
    case "sw24":
        return 43;
        break;
    case "wsw24":
        return 44;
        break;
    case "w24":
        return 45;
        break;
    case "wnw24":
        return 46;
        break;
    case "nw24":
        return 47;
        break;
    case "nnw24":
        return 48;
        break;

    default:
        return 0;
        break;
}

使用数组将“cases”存储为键,并将值返回为值。
然后可以检查数组中是否存在给定的密钥。如果是,则返回其值,如果不是,则返回默认值

使用数组将“cases”存储为键,并将值返回为值。
然后可以检查数组中是否存在给定的密钥。如果是,则返回其值,如果不是,则返回默认值

我不懂php,所以这是用python编写的:

devices = ["n2", "nne2" ...] # populate it with your devices
...

def foo(device):
    if device in devices:
        return devices.index(device) + 1
    else:
        return 0

我不懂php,所以这是在python中:

devices = ["n2", "nne2" ...] # populate it with your devices
...

def foo(device):
    if device in devices:
        return devices.index(device) + 1
    else:
        return 0

如果号码是订购的。没有比使用数组索引更好的方法了

另一方面,如果值不同,则可以使用字典/哈希映射/关联数组的简单实现来完成解决方案

在Python中,可以使用字典,如下所示

keyList = ['n2','nne2',...] 
valueList = [1, 2, 3, ...]  # Must be same size as keyList
# Best to assign both the above lists 
# with files if there are 50+ entries

deviceDict = {}

for i in range(len(keyList)):
    deviceDict[keyList[i]] = valueList[i]

如果号码是订购的。没有比使用数组索引更好的方法了

另一方面,如果值不同,则可以使用字典/哈希映射/关联数组的简单实现来完成解决方案

在Python中,可以使用字典,如下所示

keyList = ['n2','nne2',...] 
valueList = [1, 2, 3, ...]  # Must be same size as keyList
# Best to assign both the above lists 
# with files if there are 50+ entries

deviceDict = {}

for i in range(len(keyList)):
    deviceDict[keyList[i]] = valueList[i]
用PHP试试这个

function yourFunction($devices) {
    $array = ["n2","nne2","ne2","ene2","e2","ese2","se2","sse2","s2","ssw2","sw2","wsw2"];

    if($key = array_search($devices, $array))
        return $key+1;

}
用PHP试试这个

function yourFunction($devices) {
    $array = ["n2","nne2","ne2","ene2","e2","ese2","se2","sse2","s2","ssw2","sw2","wsw2"];

    if($key = array_search($devices, $array))
        return $key+1;

}
在JavaScript中:

var cases = {
   "n2"   : 1,
   "nne2" : 2,
   "ne2"  : 3,
   "ene2" : 4,
   "e2"   : 5,
   "ese2" : 6,
   // and so on
};

var myCase = cases[$device] || 0;
在JavaScript中:

var cases = {
   "n2"   : 1,
   "nne2" : 2,
   "ne2"  : 3,
   "ene2" : 4,
   "e2"   : 5,
   "ese2" : 6,
   // and so on
};

var myCase = cases[$device] || 0;

在Python中,如果返回值如此简单,则可以使用如下简单的内容:

items = ["n2","nne2","ne2","ene2", ...]
for idx, key in enumerate(items, start=1):
    if device == key:
        result = idx
    result = 0
或者,如果您期望返回值中有更复杂的内容:

my_dict = {
    "n2": 1,
    "nne2": 2,
    "ne2": 3,
    "ene2": 4,
    ...}

for key, value in my_dict.items():
    if device == key:
        result = value
    result = 0

在Python中,如果返回值如此简单,则可以使用如下简单的内容:

items = ["n2","nne2","ne2","ene2", ...]
for idx, key in enumerate(items, start=1):
    if device == key:
        result = idx
    result = 0
或者,如果您期望返回值中有更复杂的内容:

my_dict = {
    "n2": 1,
    "nne2": 2,
    "ne2": 3,
    "ene2": 4,
    ...}

for key, value in my_dict.items():
    if device == key:
        result = value
    result = 0

我建议在bash中使用索引数组:

declare -A a=([n2]='1' [nne2]='2' [ne2]='3')
device="nne2"

return "${a[$device]}"   # returns with value 2

我建议在bash中使用索引数组:

declare -A a=([n2]='1' [nne2]='2' [ne2]='3')
device="nne2"

return "${a[$device]}"   # returns with value 2

如果您需要一个独立于任何特定编程语言的解决方案,您可以尝试JSON(Javascript对象表示法)

尽管名称不同,JSON是一种与语言无关、基于文本的数据交换格式,与XML、CSV(或YAML)非常相似

由于与语言无关,JSON不仅可以通过Javascript进行查询和处理,还可以通过Java、Python、PHP和许多其他语言进行查询和处理

多种语言可以引用和处理相同的JSON数据

问题中数据的JSON示例:

{
  "2ghz": {
    "n2": 1,
    "nne2": 2,
    "ne2": 3,
    "ene2": 4,
    "e2": 5,
    "ese2": 6,
    "se2": 7,
    "sse2": 8,
    "s2": 9,
    "ssw2": 10,
    "sw2": 11,
    "wsw2": 12,
    "w2": 13,
    "wnw2": 14,
    "nw2": 15,
    "nnw2": 16
  },

  "5ghz": {
    "n5": 17,
    "nne5": 18,
    "ne5": 19,
    "ene5": 20,
    "e5": 21,
    "ese5": 22,
    "se5": 23,
    "sse5": 24,
    "s5": 25,
    "ssw5": 26,
    "sw5": 27,
    "wsw5": 28,
    "w5": 29,
    "wnw5": 30,
    "nw5": 31,
    "nnw5": 32,
  },

  "24ghz": {
    "n24": 33,
    "nne24": 34,
    "ne24": 35,
    "ene24": 36,
    "e24": 37,
    "ese24": 38,
    "se24": 39,
    "sse24": 40,
    "s24": 41,
    "ssw24": 42,
    "sw24": 43,
    "wsw24": 44,
    "w24": 45,
    "wnw24": 46,
    "nw24": 47,
    "nnw24": 48
  }
}

如果您需要一个独立于任何特定编程语言的解决方案,您可以尝试JSON(Javascript对象表示法)

尽管名称不同,JSON是一种与语言无关、基于文本的数据交换格式,与XML、CSV(或YAML)非常相似

由于与语言无关,JSON不仅可以通过Javascript进行查询和处理,还可以通过Java、Python、PHP和许多其他语言进行查询和处理

多种语言可以引用和处理相同的JSON数据

问题中数据的JSON示例:

{
  "2ghz": {
    "n2": 1,
    "nne2": 2,
    "ne2": 3,
    "ene2": 4,
    "e2": 5,
    "ese2": 6,
    "se2": 7,
    "sse2": 8,
    "s2": 9,
    "ssw2": 10,
    "sw2": 11,
    "wsw2": 12,
    "w2": 13,
    "wnw2": 14,
    "nw2": 15,
    "nnw2": 16
  },

  "5ghz": {
    "n5": 17,
    "nne5": 18,
    "ne5": 19,
    "ene5": 20,
    "e5": 21,
    "ese5": 22,
    "se5": 23,
    "sse5": 24,
    "s5": 25,
    "ssw5": 26,
    "sw5": 27,
    "wsw5": 28,
    "w5": 29,
    "wnw5": 30,
    "nw5": 31,
    "nnw5": 32,
  },

  "24ghz": {
    "n24": 33,
    "nne24": 34,
    "ne24": 35,
    "ene24": 36,
    "e24": 37,
    "ese24": 38,
    "se24": 39,
    "sse24": 40,
    "s24": 41,
    "ssw24": 42,
    "sw24": 43,
    "wsw24": 44,
    "w24": 45,
    "wnw24": 46,
    "nw24": 47,
    "nnw24": 48
  }
}

非常感谢您的贡献,我可以举出以下例子。我希望它们是他们语言的最短变体,如果不是,请随意编辑/更正

Python dev2host.py:

#!/usr/bin/env python
def dev2host(device):
    devices = [
        "n2", "nne2", "ne2", "ene2", "e2", "ese2", "se2", "sse2", "s2", "ssw2", "sw2", "wsw2", "w2", "wnw2", "nw2", "nnw2", # 2ghz
        "n5", "nne5", "ne5", "ene5", "e5", "ese5", "se5", "sse5", "s5", "ssw5", "sw5", "wsw5", "w5", "wnw5", "nw5", "nnw5", # 5ghz
        "n24", "nne24", "ne24", "ene24", "e24", "ese24", "se24", "sse24", "s24", "ssw24", "sw24", "wsw24", "w24", "wnw24", "nw24", "nnw24", # 24ghz
    ]
    for host, key in enumerate(devices, start=1):
        if device == key:
            print host
        result = 0
    return;
dev2host("ne5")
PHP dev2host.PHP:

<?php
function dev2host($device) {
    $devices = [
        "n2", "nne2", "ne2", "ene2", "e2", "ese2", "se2", "sse2", "s2", "ssw2", "sw2", "wsw2", "w2", "wnw2", "nw2", "nnw2", // 2ghz
        "n5", "nne5", "ne5", "ene5", "e5", "ese5", "se5", "sse5", "s5", "ssw5", "sw5", "wsw5", "w5", "wnw5", "nw5", "nnw5", // 5ghz
        "n24", "nne24", "ne24", "ene24", "e24", "ese24", "se24", "sse24", "s24", "ssw24", "sw24", "wsw24", "w24", "wnw24", "nw24", "nnw24", // 24ghz
    ];
    if ($host = array_search($device, $devices)) {
        return $host+1;
    }
}
echo dev2host("ne5");
?>
Javascript dev2host.js:

函数dev2host($device){
var设备={
“n2”:1,“nne2”:2,“ne2”:3,“ene2”:4,“e2”:5,“ese2”:6,“se2”:7,“sse2”:8,//2ghz
“s2”:9,“ssw2”:10,“sw2”:11,“wsw2”:12,“w2”:13,“WW2”:14,“nw2”:15,“nnw2”:16,//2ghz
“n5”:17,“nnw5”:18,“ne5”:19,“ene5”:20,“e5”:21,“ese5”:22,“se5”:23,“sse5”:24,//5ghz
“s5”:25,“ssw5”:26,“sw5”:27,“wsw5”:28,“w5”:29,“WW5”:30,“nw5”:31,“nnw5”:32,//5ghz
“n24”:33,“nnw34”:34,“ne24”:35,“ene24”:36,“e24”:37,“ese24”:38,“se24”:39,“sse24”:40,//24ghz
“s24”:41,“ssw24”:42,“sw24”:43,“wsw24”:44,“w24”:45,“WW24”:46,“nw24”:47,“nnw24”:48,//24ghz
};
var host=devices[$device]| | 0;
返回主机;
}

编写(dev2host(“ne5”)非常感谢您的贡献,我可以给出以下示例。我希望它们是他们语言的最短变体,如果不是,请随意编辑/更正

Python dev2host.py:

#!/usr/bin/env python
def dev2host(device):
    devices = [
        "n2", "nne2", "ne2", "ene2", "e2", "ese2", "se2", "sse2", "s2", "ssw2", "sw2", "wsw2", "w2", "wnw2", "nw2", "nnw2", # 2ghz
        "n5", "nne5", "ne5", "ene5", "e5", "ese5", "se5", "sse5", "s5", "ssw5", "sw5", "wsw5", "w5", "wnw5", "nw5", "nnw5", # 5ghz
        "n24", "nne24", "ne24", "ene24", "e24", "ese24", "se24", "sse24", "s24", "ssw24", "sw24", "wsw24", "w24", "wnw24", "nw24", "nnw24", # 24ghz
    ]
    for host, key in enumerate(devices, start=1):
        if device == key:
            print host
        result = 0
    return;
dev2host("ne5")
PHP dev2host.PHP:

<?php
function dev2host($device) {
    $devices = [
        "n2", "nne2", "ne2", "ene2", "e2", "ese2", "se2", "sse2", "s2", "ssw2", "sw2", "wsw2", "w2", "wnw2", "nw2", "nnw2", // 2ghz
        "n5", "nne5", "ne5", "ene5", "e5", "ese5", "se5", "sse5", "s5", "ssw5", "sw5", "wsw5", "w5", "wnw5", "nw5", "nnw5", // 5ghz
        "n24", "nne24", "ne24", "ene24", "e24", "ese24", "se24", "sse24", "s24", "ssw24", "sw24", "wsw24", "w24", "wnw24", "nw24", "nnw24", // 24ghz
    ];
    if ($host = array_search($device, $devices)) {
        return $host+1;
    }
}
echo dev2host("ne5");
?>
Javascript dev2host.js:

函数dev2host($device){
var设备={
“n2”:1,“nne2”:2,“ne2”:3,“ene2”:4,“e2”:5,“ese2”:6,“se2”:7,“sse2”:8,//2ghz
“s2”:9,“ssw2”:10,“sw2”:11,“wsw2”:12,“w2”:13,“WW2”:14,“nw2”:15,“nnw2”:16,//2ghz
“n5”:17,“nnw5”:18,“ne5”:19,“ene5”:20,“e5”:21,“ese5”:22,“se5”:23,“sse5”:24,//5ghz
“s5”:25,“ssw5”:26,“sw5”:27,“wsw5”:28,“w5”:29,“WW5”:30,“nw5”:31,“nnw5”:32,//5ghz
“n24”:33,“nnw34”:34,“ne24”:35,“ene24”:36,“e24”:37,“ese24”:38,“se24”:39,“sse24”:40,//24ghz
“s24”:41,“ssw24”:42,“sw24”:43,“wsw24”:44,“w24”:45,“WW24”:46,“nw24”:47,“nnw24”:48,//24ghz
};
var host=devices[$device]| | 0;
返回主机;
}

编写(dev2host(“ne5”)
如果每个开关案例都返回一个值,则可以从每个案例中删除
break
。如果您只是返回一个值,则字典或hashmap更适合。一种包含键和值的数据类型,您在其中提供键并将值返回给您。您刚才是说在Python中使用switch语句吗?在这种情况下不需要switch,它可以很容易地使用数组来完成。我认为没有最好的方法可以在所有语言中涵盖这种情况。例如,在python中,您更喜欢
if/else
语句而不是
switch
语句。因此,对于Python,我可以想到一个包含