如何在 JavaScript 中的数组上切换()?
How do I switch() on an array in JavaScript?
我有一个空数组如下:
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.[=17= 中的字符串]
为此,我尝试了一个 switch 语句,例如
switch(itemsWithEmptySockets){
case 0:
emptyItems.push("Head");
break;
然而,switch 语句似乎不支持以这种方式使用数组,即传递数组 itemsWithEmptySockets
.
有没有比 12 个 if 语句更有效的方法?例如
for(i=0; i < itemsWithEmptySockets.length; i++){
if(itemsWithEmptySockets[i] == 0){
emptyItems.push("Head");
}
if(itemsWithEmptySockets[i] == 1){
emptyItems.push("Neck");
}
etc.
}
非常感谢您的帮助!
我不会为此使用 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>
要回答您标题中的问题,您不能。
为您的问题提出更好的解决方案。假设各种值的值始终相同,我会将其存储在 object 中:
var parts = {
0: 'Head',
1: 'Neck',
// ...
15: 'MainHand',
16: 'SecondaryHand'
}
然后,当您想根据 api 数据填充时,请执行以下操作:
for(var i = 0; i < itemsWithEmptySockets.length; i++){
emptyItems.push(parts[itemsWithEmptySockets[i]]);
}
您还可以动态定义部分 object,如果它也可以通过 api 检索到。
另一个选项是使用 Array.prototype.map
和自定义查找方法:
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(', ');
<pre id="output"></pre>
您可以创建一个包含项目大小的数组,然后您可以在没有循环或开关的情况下索引该值。
由于这是一个很小的列表,而且索引在很小的范围内,因此创建地图似乎效率低下,但如果您的索引范围非常大,那么创建地图将是一个明智的选择。
/* 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, ' '));
body { font-family: monospace; white-space: pre; }
我有一个空数组如下:
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.[=17= 中的字符串]
为此,我尝试了一个 switch 语句,例如
switch(itemsWithEmptySockets){
case 0:
emptyItems.push("Head");
break;
然而,switch 语句似乎不支持以这种方式使用数组,即传递数组 itemsWithEmptySockets
.
有没有比 12 个 if 语句更有效的方法?例如
for(i=0; i < itemsWithEmptySockets.length; i++){
if(itemsWithEmptySockets[i] == 0){
emptyItems.push("Head");
}
if(itemsWithEmptySockets[i] == 1){
emptyItems.push("Neck");
}
etc.
}
非常感谢您的帮助!
我不会为此使用 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>
要回答您标题中的问题,您不能。
为您的问题提出更好的解决方案。假设各种值的值始终相同,我会将其存储在 object 中:
var parts = {
0: 'Head',
1: 'Neck',
// ...
15: 'MainHand',
16: 'SecondaryHand'
}
然后,当您想根据 api 数据填充时,请执行以下操作:
for(var i = 0; i < itemsWithEmptySockets.length; i++){
emptyItems.push(parts[itemsWithEmptySockets[i]]);
}
您还可以动态定义部分 object,如果它也可以通过 api 检索到。
另一个选项是使用 Array.prototype.map
和自定义查找方法:
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(', ');
<pre id="output"></pre>
您可以创建一个包含项目大小的数组,然后您可以在没有循环或开关的情况下索引该值。
由于这是一个很小的列表,而且索引在很小的范围内,因此创建地图似乎效率低下,但如果您的索引范围非常大,那么创建地图将是一个明智的选择。
/* 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, ' '));
body { font-family: monospace; white-space: pre; }