gpt4 book ai didi

javascript - 如何在 JavaScript 中对数组进行 switch() ?

转载 作者:行者123 更新时间:2023-11-28 15:23:19 25 4
gpt4 key购买 nike

我有一个空数组,如下所示:

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”添加到新数组中,然后我想用它在 DOM 中将“Head”显示为字符串。

为了做到这一点,我尝试了一个 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>

关于javascript - 如何在 JavaScript 中对数组进行 switch() ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30305077/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com