如何在JavaScript中切换数组?

时间:2015-05-18 13:52:51

标签: javascript arrays switch-statement

我有一个空数组如下:

var itemsWithEmptySockets = [];

可以填充以下一个或多个值(取决于通过n API提取的信息):

0, 1, 2, 4, 5, 6, 7, 8, 9, 15, 16

其中:

0 = Head
1 = Neck
2 = Shoulder
4 = Chest
5 = Waist
6 = Legs
7 = Feet
8 = Wrist
9 = Hands
14 = Back
15 = MainHand
16 = SecondaryHand

我想测试以查看数组中的内容,并通过将字符串存储在数组中来实际将这些数字转换为字符串,例如如果为0,那么Head有一个空套接字,将“Head”添加到新数组中,然后我想用它来显示“Head”作为DOM中的字符串。

为了做到这一点,我尝试了一个switch语句,例如

switch(itemsWithEmptySockets){
case 0:
emptyItems.push("Head");
break;

但是看来switch语句不支持以这种方式使用数组,即传递数组itemsWithEmptySockets

有没有比12 if语句更有效的方法呢? e.g。

for(i=0; i < itemsWithEmptySockets.length; i++){
    if(itemsWithEmptySockets[i] == 0){
      emptyItems.push("Head");
    }
if(itemsWithEmptySockets[i] == 1){
      emptyItems.push("Neck");
    }
etc.
}

非常感谢您的帮助!

4 个答案:

答案 0 :(得分:3)

我不会使用switch,我不认为;我使用映射对象:

// In one central place
var socketMappings = {
    0: "Head",
    1: "Neck",
    2: "Shoulder",
    4: "Chest",
    5: "Waist",
    6: "Legs",
    7: "Feet",
    8: "Wrist",
    9: "Hands",
    14: "Back",
    15: "MainHand",
    16: "SecondaryHand"
};

// Where you're doing this
var emptyItems = [];
itemsWithEmptySockets.forEach(function(entry) {
    emptyItems.push(socketMappings[entry]);
});

直播示例:

// In one central place
var socketMappings = {
  0: "Head",
  1: "Neck",
  2: "Shoulder",
  4: "Chest",
  5: "Waist",
  6: "Legs",
  7: "Feet",
  8: "Wrist",
  9: "Hands",
  14: "Back",
  15: "MainHand",
  16: "SecondaryHand"
};

// Where you're doing this
itemsWithEmptySockets = [
  0,
  14,
  5
];
var emptyItems = [];
itemsWithEmptySockets.forEach(function(entry) {
  emptyItems.push(socketMappings[entry]);
});

// Show result
snippet.log(itemsWithEmptySockets.join(", "));
snippet.log(emptyItems.join(", "));
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

如果emptyItems中还没有其他内容,请使用Array#map

// In one central place
var socketMappings = {
    0: "Head",
    1: "Neck",
    2: "Shoulder",
    4: "Chest",
    5: "Waist",
    6: "Legs",
    7: "Feet",
    8: "Wrist",
    9: "Hands",
    14: "Back",
    15: "MainHand",
    16: "SecondaryHand"
};

// Where you're doing this
var emptyItems = itemsWithEmptySockets.map(function(entry) {
    return socketMappings[entry];
});

直播示例:

// In one central place
var socketMappings = {
  0: "Head",
  1: "Neck",
  2: "Shoulder",
  4: "Chest",
  5: "Waist",
  6: "Legs",
  7: "Feet",
  8: "Wrist",
  9: "Hands",
  14: "Back",
  15: "MainHand",
  16: "SecondaryHand"
};

// Where you're doing this
itemsWithEmptySockets = [
  0,
  14,
  5
];
// Where you're doing this
var emptyItems = itemsWithEmptySockets.map(function(entry) {
  return socketMappings[entry];
});

// Show result
snippet.log(itemsWithEmptySockets.join(", "));
snippet.log(emptyItems.join(", "));
<!-- Script provides the `snippet` object, see http://meta.stackexchange.com/a/242144/134069 -->
<script src="http://tjcrowder.github.io/simple-snippets-console/snippet.js"></script>

答案 1 :(得分:1)

要回答标题中的问题,你不能。

为您的问题提出更好的解决方案。假设各种值的值始终相同,我会将其存储在对象中:

var parts = {
    0: 'Head',
    1: 'Neck',
    // ...
    15: 'MainHand',
    16: 'SecondaryHand'
}

然后,当您想根据您的api数据进行填充时,请执行以下操作:

for(var i = 0; i < itemsWithEmptySockets.length; i++){
   emptyItems.push(parts[itemsWithEmptySockets[i]]);
}

您也可以动态定义部件对象,如果它也是通过api检索的。

答案 2 :(得分:0)

另一个选择是使用Array.prototype.map和自定义查找方法:

&#13;
&#13;
function BodyPartLoookup(x){
  switch (x){
    case 0: return 'Head';
    case 1: return 'Neck';
    case 2: return 'Shoulder';
    case 4: return 'Chest';
    case 5: return 'Waist';
    case 6: return 'Legs';
    case 7: return 'Feet';
    case 8: return 'Wrist';
    case 9: return 'Hands';
    case 14: return 'Back';
    case 15: return 'MainHand';
    case 16: return 'SecondaryHand';
    default: return '';
  }
}

var originalArray = [0, 1, 2, 4, 5, 6, 7, 8, 9, 15, 16];
var resolvedArray = originalArray.map(BodyPartLoookup);

document.getElementById('output').innerHTML = resolvedArray.join(', ');
&#13;
<pre id="output"></pre>
&#13;
&#13;
&#13;

答案 3 :(得分:0)

您可以创建一个具有项目大小的数组,然后您可以在没有循环或开关的情况下索引该值。

由于这是一个如此小的列表并且索引范围很小,因此创建地图似乎效率低下,但如果您的索引范围非常大,那么创建地图将是明智的选择。

&#13;
&#13;
/* Redirect console output to HTML. */ document.body.innerHTML='';
console.log=function(){document.body.innerHTML+=[].slice.apply(arguments).join(' ')+'\n';};

const BODY_PARTS = ['Head', 'Neck', 'Shoulder', null, 'Chest', 'Waist', 'Legs', 'Feet',
    'Wrist', 'Hands', null, null, null, null, 'Back', 'MainHand', 'SecondaryHand'];

var itemsWithEmptySockets = [4, 2, 6, 8, 13, 9, 3, 5];
var emptyItems = [];

for (var i = 0; i < itemsWithEmptySockets.length; i++) {
  emptyItems.push(BODY_PARTS[itemsWithEmptySockets[i]]);
}

console.log(JSON.stringify(emptyItems, null, '  '));
&#13;
body { font-family: monospace; white-space: pre; }
&#13;
&#13;
&#13;