简体   繁体   English

如何将数组推送到递归 javascript 函数内的数组,以便在完成后可以使用它?

[英]How do I push arrays to array inside a recursive javascript function so that I can use it when it's done?

First question here (I think).这里的第一个问题(我认为)。 Please let me know if additional information is needed in order for you guys to help me out.请让我知道是否需要其他信息以便你们帮助我。

So I'm trying to implement an algorithm in javascript that uses a recursive funtion.所以我试图在 javascript 中实现一个使用递归函数的算法。

The function is copied from Implementing Heap Algorithm of Permutation in JavaScript and looks like this:该函数是从在 JavaScript 中实现堆排列算法复制而来的,如下所示:

let swap = function(array, index1, index2) {
    let temp = array[index1]
    array[index1] = array[index2]
    array[index2] = temp
    return array
}

let permutationHeap = (array, result, n) => {
    n = n || array.length // set n default to array.length
    if (n === 1) {
        result(array)
    } else {
        for (let i = 1; i <= n; i++) {
            permutationHeap(array, result, n - 1)
            if (n % 2) {
                swap(array, 0, n - 1) // when length is odd so n % 2 is 1,  select the first number, then the second number, then the third number. . . to be swapped with the last number
            } else {
                swap(array, i - 1, n - 1) // when length is even so n % 2 is 0,  always select the first number with the last number
            }
        }
    }
}


let output = function(input) {
    console.log(output)
}

permutationHeap([1,2,3,4,5], output)

The console.log in the output function (callback?) gives me the right output.输出函数(回调?)中的 console.log 给了我正确的输出。 If I move that console.log below the if statement in permutationHeap-function, I get the right output as well (console.log(array), in that case though).如果我将那个 console.log 移到 permutationHeap-function 中的 if 语句下面,我也会得到正确的输出(不过在这种情况下是 console.log(array))。

What I want to do is to store every output as an array, inside an array that I can use later on down the road.我想要做的是将每个输出作为一个数组存储在一个数组中,我以后可以使用它。 I'm guessing that I'm struggling with Javascript 101 here.我猜我在这里与 Javascript 101 苦苦挣扎。 Dealing with asynchronus thinking.处理异步思维。 But can't for the life of me figure out how to get that array of arrays!但是我终其一生都无法弄清楚如何获得该数组!

  • If I declare an empty array outside the permutationHeap-function and .push(array) it only stores [1,2,3,4,5].如果我在 permutationHeap 函数和 .push(array) 之外声明一个空数组,它只存储 [1,2,3,4,5]。 Same deal if I do the same thing inside the output-function.如果我在输出函数中做同样的事情,同样的交易。
  • I've also tried passing in an empty array to the permutationHeap-function and push that way.我还尝试将一个空数组传递给 permutationHeap 函数并以这种方式推送。 Still no luck.仍然没有运气。

Anyone who's willing to shine some light over a probably super nooby question?任何愿意为一个可能是超级菜鸟的问题发光的人? :) Much appriciated! :) 非常感谢!

I think I've managed to fix your code by using generators and yield.我想我已经通过使用生成器和产量设法修复了您的代码。 I can't exactly explain it though...虽然我不能完全解释它...

 var swap = function(array, index1, index2) { let temp = array[index1] array[index1] = array[index2] array[index2] = temp return array } var permutationHeap = function*(array, result, n) { n = n || array.length // set n default to array.length if (n === 1) { yield (array.slice(0)) } else { for (let i = 1; i <= n; i++) { yield* permutationHeap(array, result, n - 1) if (n % 2) { swap(array, 0, n - 1) // when length is odd so n % 2 is 1, select the first number, then the second number, then the third number. . . to be swapped with the last number } else { swap(array, i - 1, n - 1) // when length is even so n % 2 is 0, always select the first number with the last number } } } } var x = permutationHeap([1,2,3,4,5]) var results = Array.from(x); console.log(results);

I actually broke the algorithm in my previous answer, because by duplicating the array ever iteration I broke the part of the algorithm that relies on the array changing as it goes.实际上,我在之前的答案中破坏了算法,因为通过在每次迭代中复制数组,我破坏了依赖于数组随其变化而变化的算法部分。

I've made an answer that uses the code that you had originally more effectively:我已经做出了一个更有效地使用您最初拥有的代码的答案:

 var swap = function(array, index1, index2) { var temp = array[index1]; array[index1] = array[index2]; array[index2] = temp; return array; }; var permutationHeap = function(array, result, n) { n = n || array.length; // set n default to array.length if (n === 1) { result(array); } else { for (var i = 1; i <= n; i++) { permutationHeap(array, result, n - 1); if (n % 2) { swap(array, 0, n - 1); // when length is odd so n % 2 is 1, select the first number, then the second number, then the third number. . . to be swapped with the last number } else { swap(array, i - 1, n - 1); // when length is even so n % 2 is 0, always select the first number with the last number } } } }; function getPermutations(array) { var results = []; var output = function(res) { results.push(res.slice(0)); } permutationHeap(array, output); return results; } var permutations = getPermutations([1,2,3]); console.log(permutations);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何做一个函数,以便编程行将等到完成并移到javascript中的下一行 - How do i make a function so that the programming line will wait until it's done and move to the next line in javascript 如何在递归AJAX调用中提供“解析”功能,以便可以使用“完成”? - How can I serve a “resolve” in my recursive AJAX calls so that I can use 'done'? 为什么我无法推入数组并在javascript函数参数中使用它? - Why cant I push inside an array and use it in a function argument in javascript? 如何在 Vue 3 中推送递归数组? - How can i push recursive array in Vue 3? 如何使用 array.push() 方法,以便将数据从一个组件推送到单独文件中的数组中? - How do I can I use the array.push() method, so I can push data from one component into an array in a seperate file? 我如何使用递归 function 在 javascript 中推送数组 - How could I use the Recursion function to push array in javascript 在 Javascript 中,如何使用“push”函数定位数组条目? - in Javascript how do i target an array entry with "push" function? 如何将数组推入数组中 - How can I push array inside array 如何在另一个函数的函数中使用一个函数 - How do I use a function inside another function's function 我可以在$ .getJSON函数中使用递归JavaScript函数吗? - Can I have a recursive JavaScript function inside a $.getJSON function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM