gpt4 book ai didi

javascript - 将元素添加到已评估的循环中

转载 作者:行者123 更新时间:2023-12-02 21:58:36 25 4
gpt4 key购买 nike

下午好,我遇到以下问题,我试图循环遍历包含子目录的列表,以将这些子目录的路由添加到数组(存储库)中,这是我的代码:

        for (n=0; n<=pendingRepos.length; n++){

subruta = pendingRepos[pendingRepos.length -1]
pendingRepos.pop()
c.list(subruta, function(err, sublist) {
if (sublist.length != 0){
for (g=0; g < sublist.length; g++){

if (sublist[g].type === 'd' ){
repositories.push(subruta+'/'+sublist[g].name)
pendingRepos.push(subruta+'/'+sublist[g].name)
}

else {files.push(subruta+'/'+sublist[g].name)}
}

}

});

}

例如,当启动我的数组endingRepos 的循环时,其结构如下:

pendingRepos = ['/ dir1 / dir2', / dir3 / dir4 ']

循环正确执行了两次,并且最后一个元素被删除,但是在另一个循环将另一个“最后”元素添加到数组时,第一个 for 循环没有考虑它。我知道在添加更多元素之前已经评估了条件,这是正确的吗?我怎样才能避免它?

最佳答案

看起来您正在以两种相互矛盾的方式处理待处理的存储库数组。外层 for 循环:

for (n = 0; n <= pendingRepos.length; n++) { ... }

正在治疗pendingRepos作为一个不可变的列表,从头到尾遍历并处理每个元素。 (也没有正确执行此操作 - 如果这是我们正在使用的选项,我们应该迭代到 n < pendingRepos.length)。

然而,循环之后的逻辑,

subruta = pendingRepos[pendingRepos.length -1]
pendingRepos.pop()

请客pendingRepos作为一个可变堆栈,您将继续处理最后一个元素,直到堆栈为空。

为了正确处理数组,您需要选择其中之一。由于看起来代码的其余部分正确地使用了堆栈方法,因此应该更改顶部的循环以匹配,在本例中就是简单的

while (pendingRepos.length > 0) { ... }

最终结果如下:

while (pendingRepos.length > 0){
const subruta = pendingRepos[pendingRepos.length -1]
pendingRepos.pop()
c.list(subruta, function(err, sublist) {
if (sublist.length != 0){
for (let g = 0; g < sublist.length; g++){
if (sublist[g].type === 'd' ){
repositories.push(subruta+'/'+sublist[g].name)
pendingRepos.push(subruta+'/'+sublist[g].name)
} else {
files.push(subruta+'/'+sublist[g].name)
}
}
}
});
}
<小时/>

编辑:以上答案仅在 c.list() 时有效。是一个同步函数,在返回之前立即运行您的回调 - 但是,由于它正在联系 FTP 服务器,所以它不是。这意味着整个while循环将在任何这些回调运行之前完成,以及它们添加到 pendingRepos 的任何内容。将不会被处理。为了使用异步函数,您必须以完全不同的方式构建函数,基本上是尽可能地使用越来越多的异步函数。

幸运的是,在这种情况下做到这一点非常容易。你在做什么 pendingRepos在概念上称为 depth-first search (or "DFS") ,通过在每个子 Node 重复搜索来搜索树结构。使用挂起的目录堆栈是进行 DFS 的一种方法,另一种方法是使用递归函数(基本上每次到达目录时都会重复搜索函数)。

这是一个可能的实现,使用一直延伸的回调。

// an outer function for the whole operation. You would provide
// a callback that takes the lists of repositories and files.
function getTheRepos(startList, callbackForWholeThing) {
// build up our lists of repositories and files
const repositories = [];
const files = [];

// keep track of how many calculations are running
let repoGetCount = 0;

// an inner function to run exactly one result
function getOneRepo(subruta) {
// at the start, say we're running
repoGetCount++;
c.list(subruta, function(err, sublist) {
if (sublist.length != 0){
for (let g = 0; g < sublist.length; g++){

if (sublist[g].type === 'd' ){
repositories.push(subruta+'/'+sublist[g].name)
// for each directory we find, call this inner function again.
// This is the critical part that makes this all work.
getOneRepo(subruta+'/'+sublist[g].name)
} else {
files.push(subruta+'/'+sublist[g].name)
}
}
// at the end, say we're not running,
// and call the whole callback if we're the last one
repoGetCount--;
if (repoGetCount === 0) {
callbackForWholeThing(repositories, files);
}
}
});

// now that we have the function, run it on each of our
// start directories to start things off
for (let n = 0; n < startList.length; n++) {
getOneRepo(startList[n]);
}

// the cogs are in motion, so now return.
// The callback will be called when the tree has been searched.
}

关于javascript - 将元素添加到已评估的循环中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59940127/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com