很确定我的标题具有误导性......哦,好吧。
所以基本上,我正在尝试创建一个游戏,并且我有一堆 arrays
,其名称如 ItemsInG5Array
、ItemsInB2Array
其中 G5
和 B2
是 map 的节点。因此,我想做的是创建一个函数,该函数将为您提供玩家所在的一个特定节点中的项目列表。
function options(location)
locationPickups: while(true) {
pick = prompt("There are few items laying around.\n" + itemsInG6Array + "\nWould ou like to pick something up?");
...
其中location
是节点的名称。有什么简单的东西可以代替 ItemsInG6Array
来使其动态化,使其根据 location
变量而变化。
ItemsIn(位置)数组
最佳答案
这里的典型解决方案是拥有一个带有键的对象,然后动态选择这些键之一。
const example = {
itemsInG6Array: [],
itemsInB2Array: [],
}
function options(location) {
const key = 'itemsIn' + location + 'Array';
const items = example[key];
const pick = prompt("There are few items laying around.\n" + items + "\nWould ou like to pick something up?");
}
关于javascript - 是否可以使用函数输入来创建变量名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56394969/