简体   繁体   English

变量在循环的最后迭代中变得不确定

[英]Variable Becomes Undefined in Last Iteration of Loop

I have a application where I am getting route info from one location to another, possibly between multiple locations. 我有一个应用程序,在其中我可以从一个位置到另一位置(可能在多个位置之间)获取路线信息。 In my function, I'm looping over checked locations and writing out the directions to the page with jQuery. 在我的函数中,我遍历检查的位置并使用jQuery写出指向页面的方向。 On the last iteration of the loop the 'myRoute' variable becomes undefined. 在循环的最后一次迭代中,“ myRoute”变量变为未定义。 So, if I have three locations, it bombs on the third, but if I use those same three and add a fourth, the first three works and the fourth bombs. 因此,如果我有三个位置,则在第三个位置炸弹,但是如果我使用相同的三个位置并添加第四个,则前三个作品和第四个炸弹。 I traced the behavior in Firebug, and while the myRoute variable does get filled properly, as soon as it moves to the next line, it is all of a sudden undefined. 我跟踪了Firebug中的行为,尽管myRoute变量确实得到了正确填充,但一旦移至下一行,它就会突然变得不确定。 I removed instances of myRoute and replaced with directionResult.routes[0].legs[i] but still got the undefined error. 我删除的情况下myRoute ,取而代之的directionResult.routes[0].legs[i]但还是得到了不确定的错误。 What's going on here? 这里发生了什么?

function setInstructions(directionResult, idname, start) {
//clean tour list
$('#tours_list .tour').remove();
$('#routeTitle').show();
var checkboxArray = $(".selector.Favs" + idname).find("li");
var idx = 0;

var linkMap = $('#routeTitle').find('.link-map')[0];
linkMap.href = __mapLink+'saddr=' + start;
var firstStop = true;
//iterate selected properties
for (var i = 0; i < checkboxArray.length; i++) {
    var curChk = checkboxArray[i];
    if ($(curChk).hasClass('active')) {        
        //get steps
        var myRoute = directionResult.routes[0].legs[i]; //this is what becomes undfined
        var title = $('<div>').addClass('mileage').append($('<p>').append($('<strong>').html(myRoute.distance.text + "<br /> about " + myRoute.duration.text)));
        var ol = $('<ol>').addClass('directions');
        for (var j = 0; j < myRoute.steps.length; j++) {
            var step = myRoute.steps[j];
            var li = $('<li>').append($('<div>').addClass('direction').html(step.instructions));
            li.append($('<div>').addClass('distance').html(step.distance.text + " - " + step.duration.text));
            ol.append(li);
        }                      
        //add tour with directions
        $('#tours_list').append(temp);          
    }
}

} }

The issue is that the array in directionResult.routes[0].legs is not as long as checkboxArray.length so your code is trying to access beyond the end of directionResult.routes[0].legs and thus gets undefined . 问题在于, directionResult.routes[0].legs中的数组不如checkboxArray.length长,因此您的代码尝试访问的directionResult.routes[0].legs超出了directionResult.routes[0].legs ,因此undefined

It isn't helping you that you are testing for only active class items because i goes from 0 to checkboxArray.length - 1 regardless. 仅测试活动类项目对您没有帮助,因为i从0转到checkboxArray.length - 1无论如何)。


I don't follow exactly what you're trying to do, but you might be able to work around this by only iterating the items that have .active in the first place so i never goes beyond the number of active items. 我没有完全按照您要执行的操作进行操作,但是您也许可以通过仅迭代具有.active的项目来解决此问题,因此i永远不会超出活动项目的数量。 You might be able to do that by changing this: 您可以通过更改以下内容来做到这一点:

var checkboxArray = $(".selector.Favs" + idname).find("li");

to this: 对此:

var checkboxArray = $(".selector.Favs" + idname).find("li.active");

And, then remove the if check for the active class. 然后,删除活动类的if检查。 This will make it so your index i never goes higher than the number of active items. 这将使它所以你的索引i永远不会比主动的项目数较高。


You could also just keep a counter of the active items you've processed and use that counter to index into directionResult.routes[0].legs[cntr] instead of using i . 您也可以只保留已处理的活动项目的计数器,并使用该计数器索引到directionResult.routes[0].legs[cntr]而不是使用i

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM