最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Iterating over two arrays at a time in Javascript - Stack Overflow

programmeradmin10浏览0评论

I want to iterate over two arrays at the same time, as the values for any given index i in array A corresponds to the value in array B.

I am currently using this code, and getting undefined when I call alert(queryPredicates[i]) or alert(queryObjects[i]).
I know my array is populated as I print out the array prior to calling this.

//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]); 
    }

I want to iterate over two arrays at the same time, as the values for any given index i in array A corresponds to the value in array B.

I am currently using this code, and getting undefined when I call alert(queryPredicates[i]) or alert(queryObjects[i]).
I know my array is populated as I print out the array prior to calling this.

//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]); 
    }
Share Improve this question edited Apr 25, 2019 at 12:35 Mel 6,06510 gold badges39 silver badges42 bronze badges asked May 9, 2010 at 4:16 AnkurAnkur 51.1k114 gold badges246 silver badges316 bronze badges 8
  • What I am asking is, am I doing this the right way. If the code above looks correct, which I think it does, then that means I have some other problem. – Ankur Commented May 9, 2010 at 4:22
  • Btw I don't have any errors being detected by Firebug – Ankur Commented May 9, 2010 at 4:23
  • Hmm... Hard to see why there would be any problem with the current code—I think you'll need to provide more code. – Steve Harrison Commented May 9, 2010 at 4:23
  • Does queryPredicates have numerical indexes? – bschaeffer Commented May 9, 2010 at 4:25
  • Try alerting the actual array(s) in the loop (e.g. alert(queryPredicates);, alert(queryObjects);) and see what that gives you. – Steve Harrison Commented May 9, 2010 at 4:26
 |  Show 3 more comments

3 Answers 3

Reset to default 10

The value of the length property of any array, is the actual number of elements (more exactly, the greatest existing index plus one).

If you try to access this index, it will be always undefined because it is outside of the bounds of the array (this happens in the last iteration of your loop, because the i<=variables condition).

In JavaScript the indexes are handled from 0 to length - 1.

Aside of that make sure that your two arrays have the same number of elements.

If queryPredicates does not have numerical indexes, like 0, 1, 2, etc.. then trying to alert the value queryPredicates[0] when the first item has an index of queryPredicates['some_index'] won't alert anything.

Try using a for loop instead:

stuff['my_index'] = "some_value";

for (var i in stuff)
{
    // will alert "my_index"
    alert(i);

    // will alert "some_value"
    alert(stuff[i]);
}

Arrays in JS are zero based. Length is the actual count. Your loop is going outside the bounds of the array.

发布评论

评论列表(0)

  1. 暂无评论