如何为两个数组的内容创建所有可能的组合? [英] How can I create every combination possible for the contents of two arrays?
本文介绍了如何为两个数组的内容创建所有可能的组合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个数组:
var array1 = ["A", "B", "C"];
var array2 = ["1", "2", "3"];
如何设置另一个数组以包含上述所有组合,以便:
How can I set another array to contain every combination of the above, so that:
var combos = ["A1", "A2", "A3", "B1", "B2", "B3", "C1", "C2", "C3"];
推荐答案
这种形式的循环
combos = [] //or combos = new Array(2);
for(var i = 0; i < array1.length; i++)
{
for(var j = 0; j < array2.length; j++)
{
//you would access the element of the array as array1[i] and array2[j]
//create and array with as many elements as the number of arrays you are to combine
//add them in
//you could have as many dimensions as you need
combos.push(array1[i] + array2[j])
}
}
这篇关于如何为两个数组的内容创建所有可能的组合?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文