0

Why is name_Scenario[i] iterating each individual char instead of each string in the array?

var num_Scenarios;
var num_Features;
var name_Scenario = ["Login", "Users", "Yo", "Whatsup", "Diablo 3", "Junglr", "Microsoft", "Another", "Hi", "Anyone", "O", "Happy", "Stuff", "Sleep", "Account"];
var desc_Scenario;

function normalGrid() {
    $("#tile-bank").remove();
    $("#content").append("<div id=\"tile-bank\"></div>");
    //Code where your data is received and you run addScenario() per interation
    //filler global vars that your data will replace
    num_Scenarios = 15;
    num_Features = 20;
    name_Scenario = "Login";
    desc_Scenario = "This is a description.";
    var add_to = $("#tile-bank");
    for (var i = 0; i < num_Scenarios; i++) {
        addScenario(add_to, num_Features, name_Scenario[i], desc_Scenario);
    }
4

3 回答 3

3

name_Scenario = "Login";line 之后,name_Scenario是一个字符串,或者换句话说,是一个chars 数组,并且您正在迭代该chars。只需删除该行。

for (var i = 0; i < name_Scenario.length; i++) {
//changed 'num' to 'name'--^       ^---added '.length' here
    addScenario(add_to, num_Features, name_Scenario[i], desc_Scenario);
}
于 2012-07-18T20:05:45.277 回答
1

您正在重新定义name_Scenario. 它曾经是一个数组,但是当你用 调用它的索引时[i],它就变成了一个字符串。

此外,您不需要num_Scenarios. 您可以只使用nameScenario.length(当然,它仍然是一个数组!)。

于 2012-07-18T20:09:50.590 回答
1

您正在过度使用 var var name_Scenario;

在第 3 行你做 = ["Login", "Users", "Yo", "Whatsup", "Diablo 3", "Junglr", "Microsoft", "Another", "Hi", "Anyone", " O”、“快乐”、“东西”、“睡眠”、“账户”];

然后在循环之前执行 name_Scenario = "Login";

于 2012-07-18T20:10:08.193 回答