gpt4 book ai didi

javascript - 如何为两个数组的内容创建所有可能的组合?

转载 作者:可可西里 更新时间:2023-11-01 01:56:44 27 4
gpt4 key购买 nike

我有两个数组:

var array1 = ["A", "B", "C"];
var array2 = ["1", "2", "3"];

我怎样才能设置另一个数组来包含上面的每一个组合,这样:

var combos = ["A1", "A2", "A3", "B1", "B2", "B3", "C1", "C2", "C3"];

最佳答案

或者,如果您想使用任意数量的任意大小的数组创建组合...(我相信您可以递归地执行此操作,但由于这不是工作面试,我改为使用为此的迭代“里程表”......它根据每个数组的长度增加一个“数字”,每个数字都是一个“base-n”数字)......例如......

combineArrays([ ["A","B","C"],
["+", "-", "*", "/"],
["1","2"] ] )

...返回...

[
"A+1","A+2","A-1", "A-2",
"A*1", "A*2", "A/1", "A/2",
"B+1","B+2","B-1", "B-2",
"B*1", "B*2", "B/1", "B/2",
"C+1","C+2","C-1", "C-2",
"C*1", "C*2", "C/1", "C/2"
]

...这些中的每一个都对应于一个“里程表”值从每个数组中选择一个索引...

[0,0,0], [0,0,1], [0,1,0], [0,1,1]
[0,2,0], [0,2,1], [0,3,0], [0,3,1]
[1,0,0], [1,0,1], [1,1,0], [1,1,1]
[1,2,0], [1,2,1], [1,3,0], [1,3,1]
[2,0,0], [2,0,1], [2,1,0], [2,1,1]
[2,2,0], [2,2,1], [2,3,0], [2,3,1]

“里程计”方法让您轻松生成您想要的输出类型,而不仅仅是连接的字符串就像我们这里一样。除此之外,通过避免递归我们避免了——我敢说吗? -- 堆栈溢出...

function combineArrays( array_of_arrays ){

// First, handle some degenerate cases...

if( ! array_of_arrays ){
// Or maybe we should toss an exception...?
return [];
}

if( ! Array.isArray( array_of_arrays ) ){
// Or maybe we should toss an exception...?
return [];
}

if( array_of_arrays.length == 0 ){
return [];
}

for( let i = 0 ; i < array_of_arrays.length; i++ ){
if( ! Array.isArray(array_of_arrays[i]) || array_of_arrays[i].length == 0 ){
// If any of the arrays in array_of_arrays are not arrays or zero-length, return an empty array...
return [];
}
}

// Done with degenerate cases...

// Start "odometer" with a 0 for each array in array_of_arrays.
let odometer = new Array( array_of_arrays.length );
odometer.fill( 0 );

let output = [];

let newCombination = formCombination( odometer, array_of_arrays );

output.push( newCombination );

while ( odometer_increment( odometer, array_of_arrays ) ){
newCombination = formCombination( odometer, array_of_arrays );
output.push( newCombination );
}

return output;
}/* combineArrays() */


// Translate "odometer" to combinations from array_of_arrays
function formCombination( odometer, array_of_arrays ){
// In Imperative Programmingese (i.e., English):
// let s_output = "";
// for( let i=0; i < odometer.length; i++ ){
// s_output += "" + array_of_arrays[i][odometer[i]];
// }
// return s_output;

// In Functional Programmingese (Henny Youngman one-liner):
return odometer.reduce(
function(accumulator, odometer_value, odometer_index){
return "" + accumulator + array_of_arrays[odometer_index][odometer_value];
},
""
);
}/* formCombination() */

function odometer_increment( odometer, array_of_arrays ){

// Basically, work you way from the rightmost digit of the "odometer"...
// if you're able to increment without cycling that digit back to zero,
// you're all done, otherwise, cycle that digit to zero and go one digit to the
// left, and begin again until you're able to increment a digit
// without cycling it...simple, huh...?

for( let i_odometer_digit = odometer.length-1; i_odometer_digit >=0; i_odometer_digit-- ){

let maxee = array_of_arrays[i_odometer_digit].length - 1;

if( odometer[i_odometer_digit] + 1 <= maxee ){
// increment, and you're done...
odometer[i_odometer_digit]++;
return true;
}
else{
if( i_odometer_digit - 1 < 0 ){
// No more digits left to increment, end of the line...
return false;
}
else{
// Can't increment this digit, cycle it to zero and continue
// the loop to go over to the next digit...
odometer[i_odometer_digit]=0;
continue;
}
}
}/* for( let odometer_digit = odometer.length-1; odometer_digit >=0; odometer_digit-- ) */

}/* odometer_increment() */

关于javascript - 如何为两个数组的内容创建所有可能的组合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8936610/

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