Javascript:console.log(arr.length)表示长度为n,console.log(arr)表示长度为n-1。第二宗胜诉';不显示最后一个元素

Javascript:console.log(arr.length)表示长度为n,console.log(arr)表示长度为n-1。第二宗胜诉';不显示最后一个元素,javascript,arrays,algorithm,object,a-star,Javascript,Arrays,Algorithm,Object,A Star,第一行将打印,例如10,而第二行将少打印1: console.log("ds length: " + ds.length); // Prints, say, 10 console.log(ds); // Prints all but last element and says length is one less. 我的代码中的一些相关部分,全部在singleton中,如下所示: FindPath: function(oX, oY, dX, dY) { // origin, destinat

第一行将打印,例如10,而第二行将少打印1:

console.log("ds length: " + ds.length);  // Prints, say, 10
console.log(ds); // Prints all but last element and says length is one less.
我的代码中的一些相关部分,全部在singleton中,如下所示:

FindPath: function(oX, oY, dX, dY) { // origin, destination
        var heap = AS.Heap();

        var g = AS.Grid; // AS.Grid is declared, in AS, as: Grid: new Array(),

        var node;

        var cn = g[oX][oY]; // current node
        var dn = g[dX][dY]; // destination node

        heap.push(cn);

        cn.CoorType = AS.CoorType.VISITED;

        while (heap.length > 0) {
            cn = heap.pop();

            if (cn === dn) {
                var ds = new Array(); // direction set (instructions)
                var pn;// parent node;

                do {
                    pn = cn.Parent;
                    ds.push({
                        Dir: cn.Y - pn.Y !== 0 ? cn.Y - pn.Y < 0 ? AS.Direction.UP : AS.Direction.DOWN : cn.X - pn.X > 0 ? AS.Direction.RIGHT : AS.Direction.LEFT,
                        Node: cn});
                    cn = pn;
                } while (pn.Parent);

                console.log("ds length: " + ds.length);
                console.log(ds);

                AS.CleanUp();
                return ds;
            }

                    // Bellow, I'm not using functions 'cause occasionally, when FindPath() is called multiple times in a large area within a few milliseconds, it will get laggy. I removed the use of functions to increase performance.
            node = g[cn.X+1][cn.Y];
            if (node.CoorType === AS.CoorType.NOTHING) {
                node.CoorType = AS.CoorType.VISITED;
                node.Parent = cn;
                node.Score = ((Math.abs(node.X - oX) + Math.abs(node.Y - oY)) * AS.SMALL_ADVANTAGE + Math.abs(node.X - dX) + Math.abs(node.Y - dY)) +
                        Math.abs((node.X - dX) * (oY - dY) - (node.Y - dY) * (oX - dX)) * 0.0001;
                heap.Sink(node);
            }
            node = g[cn.X][cn.Y+1];
            if (node.CoorType === AS.CoorType.NOTHING) {
                node.CoorType = AS.CoorType.VISITED;
                node.Parent = cn;
                node.Score = ((Math.abs(node.X - oX) + Math.abs(node.Y - oY)) * AS.SMALL_ADVANTAGE + Math.abs(node.X - dX) + Math.abs(node.Y - dY)) +
                        Math.abs((node.X - dX) * (oY - dY) - (node.Y - dY) * (oX - dX)) * 0.0001;
                heap.Sink(node);
            }
            node = g[cn.X-1][cn.Y];
            if (node.CoorType === AS.CoorType.NOTHING) {
                node.CoorType = AS.CoorType.VISITED;
                node.Parent = cn;
                node.Score = ((Math.abs(node.X - oX) + Math.abs(node.Y - oY)) * AS.SMALL_ADVANTAGE + Math.abs(node.X - dX) + Math.abs(node.Y - dY)) +
                        Math.abs((node.X - dX) * (oY - dY) - (node.Y - dY) * (oX - dX)) * 0.0001;
                heap.Sink(node);
            }
            node = g[cn.X][cn.Y-1];
            if (node.CoorType === AS.CoorType.NOTHING) {
                node.CoorType = AS.CoorType.VISITED;
                node.Parent = cn;
                node.Score = ((Math.abs(node.X - oX) + Math.abs(node.Y - oY)) * AS.SMALL_ADVANTAGE + Math.abs(node.X - dX) + Math.abs(node.Y - dY)) +
                        Math.abs((node.X - dX) * (oY - dY) - (node.Y - dY) * (oX - dX)) * 0.0001;
                heap.Sink(node);
            }
        }

        AS.CleanUp();
        return heap; // No path found
    },

Heap: function() {
    var heap = new Array();

    heap.Sink = function(node) {
        var i = this.length-1;

        while (i > 0 && this[i].Score < node.Score) i--;

        this.splice(i, 0, node);
    };

    return heap;
},

CleanUp: function() { // Cleans up changes made by any previous path search
    var x, y, g = AS.Grid;

    var limX = g.length - 3;
    var limY = g[0].length - 3;

    for (x = 2; x < limX; x++) {
        for (y = 2; y < limY; y++) {
            if (g[x][y].CoorType === AS.CoorType.VISITED) {
                g[x][y].CoorType = AS.CoorType.NOTHING;
                delete g[x][y].Parent;
            }
        }
    }
}
FindPath:function(oX,oY,dX,dY){//起点,终点
var heap=AS.heap();
var g=AS.Grid;//AS.Grid在AS中声明为:Grid:new Array(),
var节点;
var cn=g[oX][oY];//当前节点
var dn=g[dX][dY];//目标节点
heap.push(cn);
cn.CoorType=AS.CoorType.visitored;
while(heap.length>0){
cn=heap.pop();
如果(cn==dn){
var ds=new Array();//方向集(指令)
var pn;//父节点;
做{
pn=cn.父母;
推({
Dir:cn.Y-pn.Y!==0?cn.Y-pn.Y<0?AS.Direction.UP:AS.Direction.DOWN:cn.X-pn.X>0?AS.Direction.RIGHT:AS.Direction.LEFT,
节点:cn});
cn=pn;
}而(家长除外),;
console.log(“ds长度:+ds.length”);
控制台日志(ds);
AS.CleanUp();
返回ds;
}
//下面,我没有使用函数,因为有时候,当FindPath()在几毫秒内在一个大区域内被多次调用时,它会变慢。我不再使用函数来提高性能。
node=g[cn.X+1][cn.Y];
if(node.CoorType==AS.CoorType.NOTHING){
node.CoorType=AS.CoorType.VISITED;
node.Parent=cn;
node.Score=((Math.abs(node.X-oX)+Math.abs(node.Y-oY))*AS.SMALL_ADVANTAGE+Math.abs(node.X-dX)+Math.abs(node.Y-dY))+
abs((node.X-dX)*(oY-dY)-(node.Y-dY)*(oX-dX))*0.0001;
Sink(节点);
}
node=g[cn.X][cn.Y+1];
if(node.CoorType==AS.CoorType.NOTHING){
node.CoorType=AS.CoorType.VISITED;
node.Parent=cn;
node.Score=((Math.abs(node.X-oX)+Math.abs(node.Y-oY))*AS.SMALL_ADVANTAGE+Math.abs(node.X-dX)+Math.abs(node.Y-dY))+
abs((node.X-dX)*(oY-dY)-(node.Y-dY)*(oX-dX))*0.0001;
Sink(节点);
}
node=g[cn.X-1][cn.Y];
if(node.CoorType==AS.CoorType.NOTHING){
node.CoorType=AS.CoorType.VISITED;
node.Parent=cn;
node.Score=((Math.abs(node.X-oX)+Math.abs(node.Y-oY))*AS.SMALL_ADVANTAGE+Math.abs(node.X-dX)+Math.abs(node.Y-dY))+
abs((node.X-dX)*(oY-dY)-(node.Y-dY)*(oX-dX))*0.0001;
Sink(节点);
}
node=g[cn.X][cn.Y-1];
if(node.CoorType==AS.CoorType.NOTHING){
node.CoorType=AS.CoorType.VISITED;
node.Parent=cn;
node.Score=((Math.abs(node.X-oX)+Math.abs(node.Y-oY))*AS.SMALL_ADVANTAGE+Math.abs(node.X-dX)+Math.abs(node.Y-dY))+
abs((node.X-dX)*(oY-dY)-(node.Y-dY)*(oX-dX))*0.0001;
Sink(节点);
}
}
AS.CleanUp();
返回堆;//找不到路径
},
堆:函数(){
var heap=新数组();
heap.Sink=函数(节点){
var i=该长度-1;
而(i>0&&this[i].Score
我意识到,有时,我的对象应该按照FindPath返回的路径移动,但会错过一步。然后我看到了我上面所描述的


通过在do{}while(pn.Parent)中添加两行:

do{
pn=cn.父母;
推({
Dir:cn.Y-pn.Y!==0?cn.Y-pn.Y<0?AS.Direction.UP:AS.Direction.DOWN:cn.X-pn.X>0?AS.Direction.RIGHT:AS.Direction.LEFT,
节点:cn});
console.log(ds.length);//添加了这一行,
console.log(ds);//还有这个
cn=pn;
}而(家长除外),;
我意识到console.log(ds)在调用GameLoop()后打印ds,GameLoop()在某个时候会调用as.FindPAth,而console.log(ds.length)在打印ds.length时打印的是我要求打印时的样子

因为,在GameLoop()结束之前,我正在删除返回我的FindPath()的路径的最后一个元素(nextDirection=path.pop()),console.log(ds)会打印自己,但缺少一个元素

尽管如此,javascript在执行诸如按稍后的方式打印对象的状态而不是按console.log()时的方式打印对象的状态之类的操作时还是让人抓狂

do {
    pn = cn.Parent;
    ds.push({
        Dir: cn.Y - pn.Y !== 0 ? cn.Y - pn.Y < 0 ? AS.Direction.UP : AS.Direction.DOWN : cn.X - pn.X > 0 ? AS.Direction.RIGHT : AS.Direction.LEFT,
        Node: cn});
    console.log(ds.length); // Added this line,
    console.log(ds);        // and this one
    cn = pn;
} while (pn.Parent);