English 中文(简体)
无法利用 lo获得阵列价值
原标题:Unable to get array value using a for loop

我在这样的阵列中增加价值观:

ansArray = {question:id[count], answer:  a };

and和ole显示出这样的结果:

(
    {
        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
}

出现这种错误,没有界定;

但如果不使用变数,而是使用一定数字,则该信息显示正确的价值:

 alert(ansArray[0].answer); //shows correct value
 alert(ansArray[1].question); //shows correct value

为什么没有漏洞显示错误? 我在这里失踪了什么?

感谢您的帮助。

问题回答

你的第一个问题是,你的阿雷米变量不是阵列,而是物体。 你宣布如下:

ansArray = {question:id[count], answer:  a };

这是一项带有问题领域和答案的物体。 您可以宣布:

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 tag也是有益的。





相关问题
WordPress Data Storage Efficiency

I ve been asked to review a WordPress plugin of sorts and try to find ways of making it faster. The premise of this plugin is basically to store a bunch of users and shifts and appointments and ...

Convert a 2D array index into a 1D index

I have two arrays for a chess variant I am coding in java...I have a console version so far which represents the board as a 1D array (size is 32) but I am working on making a GUI for it and I want it ...

Convert an array of integers for use in a SQL "IN" clause

Surely there is a framework method that given an array of integers, strings etc converts them into a list that can be used in a SQL "IN" clause? e.g. int[] values = {1,2,3}; would go to "(1,2,3)"

Sorting twodimensional Array in AS3

So, i have a two-dimensional Array of ID s and vote count - voteArray[i][0] = ID, voteArray[i][1] = vote count I want the top 3 voted items to be displayed in different colors, so i have a 2nd Array -...

C++ Array Sort Me

Stuck on an array sorter. Have to sort numbers from largest to smallest. I m trying two loops (one nested in the other). Here s the code: int counter=0; // inner counter int counter2=0; // outer ...

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

Best practice of big javascript objects

sry for this imprecise topic name. I am querying a dataset a lot of times so using ajax request would end up in tons of http requests. For this reason I decided to use the json encode method to ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

热门标签