Ank*_*kur 4 javascript arrays iteration
我想同时迭代两个数组,因为数组A中任何给定索引i的值对应于数组B中的值.
我目前正在使用这段代码,并i在我调用时获取undefined或者alert(queryPredicates[i]),我知道我的数组已经填充,因为我在调用之前打印出数组,我没有把所有其他代码放入,因为它可能会令人困惑,但如果你认为问题不明显,我将编辑问题:
//queryPredicates[] and queryObjects[] are defined above as global vars - not in a particular function, and I have checked that they contain the correct information.
function getObjectCount(){
var variables = queryPredicates.length; //the number of variables is found by the length of the arrays - they should both be of the same length
var queryString="count="+variables;
for(var i=1; i<=variables;i++){
alert(queryPredicates[i]);
alert(queryObjects[i]);
}
Run Code Online (Sandbox Code Playgroud)
谢谢
CMS*_*CMS 10
length任何数组的属性值都是元素的实际数量(更准确地说,是现有最大的索引加上一个).
如果您尝试访问此索引,它将始终undefined是因为它超出了数组的边界(这发生在循环的最后一次迭代中,因为i<=variables条件).
在JavaScript中的索引,从处理0到length - 1.
除此之外,请确保您的两个数组具有相同数量的元素.