我在连接数组时遇到了一些麻烦。不知道如何最好地解释这一点,但基本上我希望新节点的每次迭代都从中获取数组node.prevNodes并将新数组推送到具有先前内容的数组中。现在它正在做那种 - 排序 - 但不是多维数组,它只是将所有值作为字符串放入数组中。请参阅下面的详细信息。
function node(posX, posY, numMoves, prevNodes){
this.posX = posX;
this.posY = posY;
this.numMoves = numMoves;
this.prevNodes = [prevNodes];
}
nodes = []; // all nodes
nodes.push( new node(1, 1, 0) ); // add starting node
while (nodes.length != 0) {
currentPos = nodes.shift();
for (i = 0; i < 8; i++) {
newArr = currentPos.prevNodes + [x, y]; /* I think this is the issue */
nodes.push( new node(x, y, d+1, newArr) );
// Example Ideal value for newArr (after some iteration):
// [[3,2],[5,3],[7,4],[6,2],[8,1]]
// Actual value:
// ["3,25,33,42,6"]
}
}
所有其余的代码都是任意的,以便为发生的事情提供一些背景信息。我认为要么newArr是创建方式,要么是prevNodes参数的发送方式。被这里绊倒了……