How can I create every combination possible for the contents of two arrays?

前端 未结 10 1422
南旧
南旧 2020-11-27 20:44

I have two arrays:

var array1=[\"A\",\"B\",\"C\"];

var array2=[\"1\",\"2\",\"3\"];

How can I set another array to contain every combinatio

10条回答
  •  南方客
    南方客 (楼主)
    2020-11-27 21:26

    A loop of this form

    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])
         }
    }
    

提交回复
热议问题