javascript - 在递归循环中将值推送到全局数组
问题描述
我对 JavaScript 有点陌生。如何将新值推送到数组路由。似乎由于某种原因,在递归结束后调用了 push 函数。
我知道我必须使用回调闭包,但在实现它们时遇到问题并且路由的值正确反映
下面是我想要推送到我的路由数组的输出
[0,1,3,4]
[0,2,4]
[0,3,4]
相反,我得到
[[0],[0],[0]]
请在下面提供帮助是我评论了错误来自的区域的代码。
var popMap;
var routes = [];
function removeA(arr) {
var what, a = arguments, L = a.length, ax;
while (L > 1 && arr.length) {
what = a[--L];
while ((ax= arr.indexOf(what)) !== -1) {
arr.splice(ax, 1);
}
}
return arr;
}
const initPopMap = (numPops) => {
popMap = Array(numPops);
for(var i = 0; i < numPops; i++ ){
popMap[i] = [];
}
}
const addEdge = (origin, destination) => {
popMap[origin].push(destination);
}
const getRoutes = (origin,destination,visitedPops,routePath) => {
visitedPops[origin] = true;
if(origin == destination){
console.log(JSON.stringify(routePath))
////////////////////////////
////////////////////////////
////BUG IS HERE//////////////////////
routes.push(routePath);
///////////////////////////////////
/////////////////////////////////
visitedPops[origin] = false;
return;
}
for (let index = 0; index < popMap[origin].length; index++) {
const endPop = popMap[origin][index];
if(visitedPops[endPop] == false){
//console.log("PAHTLIST preadd " + routePath);
routePath.push(endPop);
//console.log("Post add " + endPop);
//console.log("PAHTLIST postadd " + routePath);
getRoutes(endPop,destination,visitedPops,routePath);
//console.log("PAHTLIST prepop " + routePath);
routePath = removeA(routePath,endPop);
//console.log("post POp " + endPop);
//console.log("PAHTLIST postpop " + routePath);
}
}
visitedPops[origin] = false;
}
const routeDiscover = (origin,destination) => {
var numOfPops = 5;
var routePath = [];
var visitedPops = Array(numOfPops); //This array keeps track of visited nodes
for(var i = 0; i < visitedPops.length; i++){
visitedPops[i] = false;
}
initPopMap(numOfPops);
addEdge(0,1);
addEdge(0,2);
addEdge(0,3);
addEdge(1,0);
addEdge(1,3);
addEdge(2,0);
addEdge(2,4);
addEdge(3,0);
addEdge(3,1);
addEdge(3,4);
addEdge(4,2);
addEdge(4,3);
routePath.push(origin);
getRoutes(origin,destination,visitedPops,routePath);
console.log(JSON.stringify(routes));
}
routeDiscover(0,4)```
解决方案
我不确定这个函数的上下文,所以我不知道a = arguments
它是干什么用的。如果您将其替换为有效变量,则返回将是您所期望的。
let popMap;
let routes = [];
const removeA = (arr) => {
var what, a = [], L = a.length, ax;
while (L > 1 && arr.length) {
what = a[--L];
while ((ax= arr.indexOf(what)) !== -1) {
arr.splice(ax, 1);
}
}
return arr;
}
const initPopMap = (numPops) => {
popMap = Array(numPops);
for(var i = 0; i < numPops; i++ ){
popMap[i] = [];
}
}
const addEdge = (origin, destination) => {
popMap[origin].push(destination);
}
const getRoutes = (origin,destination,visitedPops,routePath) => {
visitedPops[origin] = true;
if(origin == destination){
console.log(JSON.stringify(routePath))
routes.push(routePath);
visitedPops[origin] = false;
return;
}
for (let index = 0; index < popMap[origin].length; index++) {
const endPop = popMap[origin][index];
if(visitedPops[endPop] == false){
//console.log("PAHTLIST preadd " + routePath);
routePath.push(endPop);
//console.log("Post add " + endPop);
//console.log("PAHTLIST postadd " + routePath);
getRoutes(endPop,destination,visitedPops,routePath);
//console.log("PAHTLIST prepop " + routePath);
routePath = removeA(routePath,endPop);
//console.log("post POp " + endPop);
//console.log("PAHTLIST postpop " + routePath);
}
}
visitedPops[origin] = false;
}
const routeDiscover = (origin,destination) => {
var numOfPops = 5;
var routePath = [];
var visitedPops = Array(numOfPops); //This array keeps track of visited nodes
for(var i = 0; i < visitedPops.length; i++){
visitedPops[i] = false;
}
initPopMap(numOfPops);
addEdge(0,1);
addEdge(0,2);
addEdge(0,3);
addEdge(1,0);
addEdge(1,3);
addEdge(2,0);
addEdge(2,4);
addEdge(3,0);
addEdge(3,1);
addEdge(3,4);
addEdge(4,2);
addEdge(4,3);
routePath.push(origin);
getRoutes(origin,destination,visitedPops,routePath);
console.log(JSON.stringify(routes));
}
routeDiscover(0,4);
推荐阅读
- python-3.x - ValueError:AES 密钥的长度必须为 16、24 或 32 字节。KEY 大小没有意义
- python - 来自(可能)递归函数的 Pandas 新列
- r - 如何根据来自第二个数据帧的部分字符串对数据帧进行子集化?
- javascript - 传播语法与切片方法
- python - 多处理 Pandas SQL
- matrix - Octave / MATLAB中的快速矩阵求和?
- autohotkey - AutoHotKey 发送热键
- apache-spark - 使用没有整数列的 Spark(并行)从关系数据库中提取数据?
- c# - WinSCP .net 程序集不遵守包含文件掩码
- asp.net - SQL Server Express 连接到 .mdf 文件