Arrays 无法使用for循环获取数组值
我向数组中添加如下值:Arrays 无法使用for循环获取数组值,arrays,loops,undefined,Arrays,Loops,Undefined,我向数组中添加如下值: ansArray = {question:id[count], answer: 'a'}; ( { answer = a; question=1; }, { answer =d; question=2; } //...... //....... ) for (i=0;i<20;i++){ alert(ansArray[i].answer);
ansArray = {question:id[count], answer: 'a'};
(
{
answer = a;
question=1;
},
{
answer =d;
question=2;
}
//......
//.......
)
for (i=0;i<20;i++){
alert(ansArray[i].answer); //This comes with undefined error
alert(ansArray[i].question); //undefined
}
someObject = {};
someArray = [];
ansArray = [{question:0, answer:'a'}];
var ansArray = [];
ansArray[0] = {question:0, answer:'a'};
ansArray[1] = {question:1, answer:'d'};
...
ansArray[19] = {question:19, answer:'b'};
控制台显示的结果如下:
ansArray = {question:id[count], answer: 'a'};
(
{
answer = a;
question=1;
},
{
answer =d;
question=2;
}
//......
//.......
)
for (i=0;i<20;i++){
alert(ansArray[i].answer); //This comes with undefined error
alert(ansArray[i].question); //undefined
}
someObject = {};
someArray = [];
ansArray = [{question:0, answer:'a'}];
var ansArray = [];
ansArray[0] = {question:0, answer:'a'};
ansArray[1] = {question:1, answer:'d'};
...
ansArray[19] = {question:19, answer:'b'};
我使用了这样的for循环:
ansArray = {question:id[count], answer: 'a'};
(
{
answer = a;
question=1;
},
{
answer =d;
question=2;
}
//......
//.......
)
for (i=0;i<20;i++){
alert(ansArray[i].answer); //This comes with undefined error
alert(ansArray[i].question); //undefined
}
someObject = {};
someArray = [];
ansArray = [{question:0, answer:'a'}];
var ansArray = [];
ansArray[0] = {question:0, answer:'a'};
ansArray[1] = {question:1, answer:'d'};
...
ansArray[19] = {question:19, answer:'b'};
为什么for循环没有显示错误?我错过了什么
谢谢您的帮助。您的第一个问题是AnArray变量不是数组,而是对象。您将按如下方式声明它:
ansArray = {question:id[count], answer: 'a'};
这将创建一个包含问题字段和答案字段的对象。您可以这样声明对象:
ansArray = {question:id[count], answer: 'a'};
(
{
answer = a;
question=1;
},
{
answer =d;
question=2;
}
//......
//.......
)
for (i=0;i<20;i++){
alert(ansArray[i].answer); //This comes with undefined error
alert(ansArray[i].question); //undefined
}
someObject = {};
someArray = [];
ansArray = [{question:0, answer:'a'}];
var ansArray = [];
ansArray[0] = {question:0, answer:'a'};
ansArray[1] = {question:1, answer:'d'};
...
ansArray[19] = {question:19, answer:'b'};
您可以这样声明数组:
ansArray = {question:id[count], answer: 'a'};
(
{
answer = a;
question=1;
},
{
answer =d;
question=2;
}
//......
//.......
)
for (i=0;i<20;i++){
alert(ansArray[i].answer); //This comes with undefined error
alert(ansArray[i].question); //undefined
}
someObject = {};
someArray = [];
ansArray = [{question:0, answer:'a'}];
var ansArray = [];
ansArray[0] = {question:0, answer:'a'};
ansArray[1] = {question:1, answer:'d'};
...
ansArray[19] = {question:19, answer:'b'};
所以,你可能想得到这样的东西:
ansArray = {question:id[count], answer: 'a'};
(
{
answer = a;
question=1;
},
{
answer =d;
question=2;
}
//......
//.......
)
for (i=0;i<20;i++){
alert(ansArray[i].answer); //This comes with undefined error
alert(ansArray[i].question); //undefined
}
someObject = {};
someArray = [];
ansArray = [{question:0, answer:'a'}];
var ansArray = [];
ansArray[0] = {question:0, answer:'a'};
ansArray[1] = {question:1, answer:'d'};
...
ansArray[19] = {question:19, answer:'b'};
这将为您提供一个包含单个对象的数组。我猜你想要这样的东西:
ansArray = {question:id[count], answer: 'a'};
(
{
answer = a;
question=1;
},
{
answer =d;
question=2;
}
//......
//.......
)
for (i=0;i<20;i++){
alert(ansArray[i].answer); //This comes with undefined error
alert(ansArray[i].question); //undefined
}
someObject = {};
someArray = [];
ansArray = [{question:0, answer:'a'}];
var ansArray = [];
ansArray[0] = {question:0, answer:'a'};
ansArray[1] = {question:1, answer:'d'};
...
ansArray[19] = {question:19, answer:'b'};
您将能够使用原始循环代码对此进行循环。使用javascript标记也会很有帮助