我尝试用 Google 搜索这个,但很难解释我想要做什么。
我返回了一个字符串var returnedString = model one
。
假设我有一堆名为
的数组var model_one = ['a', 'b', 'c']
var model_two = ['d', 'e', 'f']
var model_three = ['f', 'g', 'h']
这就是我所做的
var selectedArrayName = returnedString.replace(" ", "_") //model_one
这就是我想做的......
for (i in array with name selectedArrayName) {
print selectedArrayName's array index.
}
最佳答案
解决此问题的一种方法是创建模型字典,可以通过括号表示法[]
访问该字典:
var models = {
model_one: ['a', 'b', 'c'],
model_two: ['d', 'e', 'f'],
model_three: ['f', 'g', 'h']
};
var modelName = 'model_one';
for(var index in models[modelName]) {
var content = models[modelName][index];
console.log(index, content);
}
关于javascript - 如何迭代其变量名称取决于不同变量的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41965192/