首页 > 解决方案 > 在递归循环中将值推送到全局数组

问题描述

我对 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)```

标签: javascriptarraysrecursion

解决方案


我不确定这个函数的上下文,所以我不知道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);


推荐阅读