我怎样才能为两个数组的内容创建可能的组合? [英] How can I create every combination possible for the contents of two arrays?

查看:136
本文介绍了我怎样才能为两个数组的内容创建可能的组合?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数组:

I have 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆