Javascript 将数组添加到数组列表中为空
所以我写了一个方法来递归地找到一系列数字的所有排列 我将每次运行的结果(一个数字数组)添加到一个更大的Javascript 将数组添加到数组列表中为空,javascript,recursion,Javascript,Recursion,所以我写了一个方法来递归地找到一系列数字的所有排列 我将每次运行的结果(一个数字数组)添加到一个更大的组合中。然而,组合正变得充满这样的空数组:[[],[],[]]而不是[[1,2,3],[3,2,1],[2,3,1]…],但我知道组合正在生成 以下是我所拥有的: var combos = []; permuteHelper([1,2,3],[]); console.log(combos); //printing blank array or arrays [[],[]] function pe
组合中。然而,组合正变得充满这样的空数组:[[],[],[]]
而不是[[1,2,3],[3,2,1],[2,3,1]…]
,但我知道组合正在生成
以下是我所拥有的:
var combos = [];
permuteHelper([1,2,3],[]);
console.log(combos); //printing blank array or arrays [[],[]]
function permuteHelper(list, combination){
if(list.length == 0){
combos.push(combination); //ERROR: combos only holds blank arrays (e.g. [[],[]] )
console.log(combination); //although this is printing correct list after each recursive run (e.g. [3,2,1] )
}
for(var i = 0; i < list.length; i++ ){
//pick a digit
var digit = list[i];
list.splice(i,1); //remove the character from the array
combination.push(digit);
//recursively keep picking
permuteHelper(list, combination);
//backtrack and put the digit back for next time
list.splice(i,0,digit);
combination.pop();
}
}
但是组合仍然没有正确填充。我是js新手,不知道我遗漏了什么。当您将组合作为函数的参数时,它将被视为引用。对组合的任何更改
都将更改原始变量。您将该引用移交给permuteHelper
的每个新调用,因此始终修改原始数组
代码更改很少的原始解决方案是:
var combos = [];
permuteHelper([1, 2, 3], [], []);
function permuteHelper(list, _combination) {
var combination = Object.assign([], _combination);
if (list.length == 0) {
combos.push(combination); //this only has a blank array of
arrays(e.g. [[], []])
console.log(combination); //prints correct permuted list after each recursive trial (e.g. [3,2,1] )
}
for (var i = 0; i < list.length; i++) {
//pick a digit
var digit = list[i];
list.splice(i, 1); //remove the character from the array
combination.push(digit);
//recursively keep picking
permuteHelper(list, combination, combos);
//backtrack and put the digit back for next time
list.splice(i, 0, digit);
combination.pop();
}
}
var组合=[];
permuteHelper([1,2,3],],[]);
函数permuteHelper(列表、组合){
变量组合=对象分配([],_组合);
如果(list.length==0){
combos.push(composition);//这只有一个
阵列(例如[…]、[…])
console.log(组合);//在每次递归尝试后打印正确的排列列表(例如[3,2,1])
}
对于(变量i=0;i
这样,您就可以从参数\u composition
创建一个新的对象composition
,该对象可以随意修改。当您将composition
作为函数的参数时,它将被视为一个引用。对组合的任何更改
都将更改原始变量。您将该引用移交给permuteHelper
的每个新调用,因此始终修改原始数组
代码更改很少的原始解决方案是:
var combos = [];
permuteHelper([1, 2, 3], [], []);
function permuteHelper(list, _combination) {
var combination = Object.assign([], _combination);
if (list.length == 0) {
combos.push(combination); //this only has a blank array of
arrays(e.g. [[], []])
console.log(combination); //prints correct permuted list after each recursive trial (e.g. [3,2,1] )
}
for (var i = 0; i < list.length; i++) {
//pick a digit
var digit = list[i];
list.splice(i, 1); //remove the character from the array
combination.push(digit);
//recursively keep picking
permuteHelper(list, combination, combos);
//backtrack and put the digit back for next time
list.splice(i, 0, digit);
combination.pop();
}
}
var组合=[];
permuteHelper([1,2,3],],[]);
函数permuteHelper(列表、组合){
变量组合=对象分配([],_组合);
如果(list.length==0){
combos.push(composition);//这只有一个
阵列(例如[…]、[…])
console.log(组合);//在每次递归尝试后打印正确的排列列表(例如[3,2,1])
}
对于(变量i=0;i
这样,您就可以根据参数\u composition
创建一个新对象composition
,可以根据需要进行修改。能否提供更多详细信息,特别是您希望最终输出的外观和实际得到的内容?是的!所以如果我打印组合,我希望得到这样的输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1],[3,2,1],[3,2,1],[3,2,1]。但是,打印组合看起来更像这样:[[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]!因此,如果我打印组合,我希望得到如下输出:[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1],[3,2,1]]但是,打印组合看起来更像:[[],[],[],[],[],[],[],[],[]但是我确实想修改我的原始数组。我希望它能保存找到的所有组合。在每次迭代完成后,我想把这个新值附加到它上面。请看我更新的答案,这是完全相同的问题,只是一个不同的变量。太棒了!谢谢,但我确实想修改我原来的数组。我希望它能保存找到的所有组合。在每次迭代完成后,我想把这个新值附加到它上面。请看我更新的答案,这是完全相同的问题,只是一个不同的变量。太棒了!非常感谢。