Javascript Can';在对另一个数组排序后,无法访问任何数组

Javascript Can';在对另一个数组排序后,无法访问任何数组,javascript,sorting,multidimensional-array,Javascript,Sorting,Multidimensional Array,我遇到了一个非常奇怪的错误: 我从另一个数组(tours[])派生出一个新数组allSavings[]),并在函数calculateAllSavings()中对其排序。在调用函数之前,我可以访问tours[]很好,但是之后,我就不能再访问了。div标签demo1和demo2都存在,并且对于其他输出工作正常 function euclDist(node1,node2){ if(node1 != node2){ var x = Math.pow(nodes[node2].x

我遇到了一个非常奇怪的错误: 我从另一个数组(
tours[]
)派生出一个新数组
allSavings[]
),并在函数
calculateAllSavings()
中对其排序。在调用函数之前,我可以访问
tours[]
很好,但是之后,我就不能再访问了。div标签
demo1
demo2
都存在,并且对于其他输出工作正常

function euclDist(node1,node2){
    if(node1 != node2){
        var x = Math.pow(nodes[node2].x - nodes[node1].x,2);
        var y = Math.pow(nodes[node2].y - nodes[node1].y,2);
        var dist = Math.sqrt(x+y);
        return dist;
    }
    else return 0.0;
}

function tourDist(members){
    var tourDist = 0.0;
    if (members.length>1){
            for (i = 1; i < members.length; i++)
                tourDist += euclDist(members[i],members[i-1]);
    }
    return tourDist;
}

function combineTours(tourA, tourB){
    tourA.pop();
    tourB.shift();
    return tourA.concat(tourB);
}

function calculateSaving(tourA,tourB){
    var costSeparate = tourDist(tourA) + tourDist(tourB);
    var combTour = combineTours(tourA,tourB);
    var costCombined = tourDist(combTour);
    return costSeparate - costCombined;
}

function calculateAllSavings(){
    var allPossibilities = [];
    for(var i = 0; i < tours.length; i++){
        for(var j = 0; j < tours.length; j++){
            if(i != j)
            var savingObj = {saving:calculateSaving(tours[i],tours[j]), tourA: i, tourB: j};
                allPossibilities.push(savingObj);
        }
    }
    allPossibilities.sort(function(a, b){
        return b.saving-a.saving
    })  
    document.getElementById("demo3").innerHTML = "success";

    return allPossibilities;
}

//Initialize Array  
var tours = [];
tours.push([0,1,2,3,0]);
tours.push([0,4,5,6,0]);
tours.push([0,7,8,0]);
tours.push([0,9,10,0]);

//BUG
document.getElementById("demo1").innerHTML = tours.join('\n'); // Shows array correctly
var allSavings = calculateAllSavings(); //BUG APPEARS HERE
document.getElementById("demo2").innerHTML = tours.join('\n'); // Doesn't show anything
谢谢所有帮助我的人你能试试这个吗

for(var i = 0; i < tours.length; i++){
        for(var j = 0; j < tours[i].length; j++){
            if(i != j)
            var savingObj = {saving:calculateSaving(tours[i],tours[j]), tourA: i, tourB: j};
                allPossibilities.push(savingObj);
        }
    }
for(var i=0;i

除此之外,您还可以调试并查看您的文档.getElementById(“demo2”).innerHTML=tours.join('\n');行实际执行。您可能正在运行一个无限循环。尝试使用chrome开发者工具调试代码。

好的,在
combineTours
函数中,您在一个数组上调用
.pop()
方法,在另一个数组上调用
.shift()
方法,这将从每个数组中删除一个元素。在
calculateAllSavings
中,您在一个循环中调用
calculateSaving
,它调用
combineTours
,因此可以有效地从子数组中删除所有元素

也许您应该将这些行从
combineTours
中删除:

tourA.pop();
tourB.shift();

将来:用于调试,它可以帮助您识别问题。

您是否也可以包括
tourDist
函数的代码?此外,您应该始终在条件之后编写包含
{}
的if语句,即使它是一行。@matthewhbst@Gothdo他的代码不换行(好吧,他故意把它放在下一行),这就是我的评论。谢谢你的回复。我添加了tourDist()和euclDist()方法。很抱歉格式不好,这是一种一次性使用的黑客代码,因此不需要维护性…我怀疑是缺少括号。内部for循环应该通过外部for循环。因此j应该通过i中的第一个数组,然后是第二个数组,然后是第三个数组。您所做的是,重复j数组,与s完全相同ame作为i数组。因此没有对内部值进行排序。@Samuel:)不客气。你也能接受我的回答吗?对不起,我的回答有点太快了。我之所以能够再次访问tours[],只是因为j循环中的语句从未执行过。通过这种方式calculateAllSavings()提供了一个空数组:-(您好,谢谢您的响应。我需要pop()和shift()因为每个巡更都需要在第一个和最后一个节点中有数字0。因此,如果我有tourA 01230和tourB 04560,它们应该组合成01234560。@Samuel所以每次巡更只调用一次。因此,您的回答让我找到了正确的路径:在combineTours()内,我克隆了tourA和tourB,并使用.slice(0)从而保留了原始的旅行[]原封不动。非常感谢,我也会在原始帖子中编辑它
tourA.pop();
tourB.shift();