3

i'm currently stuck on this problem,

suppose there is an array arr4(length is not fixed). I need to find all combinations of its element.

expected output is:

4-16D-5d 
4-16D-5e
4A-16D-5d
4A-16D-5e
4B-16D-5d
4B-16D-5e

4-15D-5d
4-15D-5e
4A-15D-5d
4A-15D-5e
4B-15D-5d
4B-15D-5e

What i tried is:

arr4=[['4','4A','4B'],['16D','15D'],['5d','5e']]; //may contains more elements
alert("arr4 "+arr4);
for(var k=0;k<arr4.length;k++){
    alert("k "+k);
    arr_v=arr4[k]
    alert("arrv"+arr_v);
    alert ("arrv lengt"+arr_v.length);
    for(var z=0;z<arr_v.length;z++){
        m=1;
        while(m<arr4.length){
            var test=arr4[m];
            for(var n=0;n<test.length;n++)
                alert(arr_v[z]+"<TEST>"+test[n]);
        }
        m++;
    }
}   

I kind of found a solution:

function product() {
    return Array.prototype.reduce.call(arguments, function(as, bs) {
        return [a.concat(b) for each (a in as) for each (b in bs)]
    }, [[]]);
}

BUT

arr4=[['4','4A','4B'],['16D','15D'],['5d','5e']];
alert(product(['4','4A','4B'],['16D','15D'],['5d','5e']);

The above works but the following don't work:

arr4=[['4','4A','4B'],['16D','15D'],['5d','5e']];
alert(product(arr4);

Anyone please suggest a solution

edit:solution here

Community
  • 1
  • 1
j.b
  • 151
  • 1
  • 7
  • 17

2 Answers2

2
var arr4 = [['4','4A','4B'],['16D','15D'],['5d','5e']];
for(var a = 0; a < arr4[1].length; a++){
    if(a != 0){
        document.write('<br /><br />');
    }
    for(var b = 0; b < arr4[0].length; b++){
        for(var c = 0; c < arr4[2].length; c++){
            document.write(arr4[0][b] + '-' + arr4[1][a] + '-' + arr4[2][c] + '<br />')
        }
    }
}

Obviously you don't want to use document.write but inside the last for loop is where each lines output will be using

arr4[0][b] + '-' + arr4[1][a] + '-' + arr4[2][b]

jsFiddle for those interested http://jsfiddle.net/5AmMU/1/

James Hay
  • 7,115
  • 6
  • 33
  • 57
  • Thanks a lot for your solution it works as expected. but this is where i'm stuck. the arr4 is not a fixed array. It gets poulated with random generated data. On different initialisations it may be as follows [['4','4A','4B'],['16D','15D'],['5d','5e'],['6d','8d'],['9g','9h',['9i']]); or [['4','4A','4B'],['16D','15D'])]. – j.b Feb 21 '12 at 20:12
  • Well that would be fine if you were writing 'a' for arr4[0], b for arr4[1], c for arr4[2]. But you want them written in a specific order, how are you going to know the order you want them written in when it is a dynamically filled array? – James Hay Feb 21 '12 at 20:17
2

You can compute the cartesian product of each sub-array. See this question and its answer

I tried it out, and since it goes off the length of each array, it should work no matter how many elements are in each list and return you a list of each product.

Community
  • 1
  • 1
Charlie G
  • 814
  • 9
  • 22
  • It seems to work but why: arr4=[['4','4A','4B'],['16D','15D'],['5d','5e']]; alert(product(arr4); Don't work but alert(product(['4','4A','4B'],['16D','15D'],['5d','5e']); works. Any suggestions? thanks – j.b Feb 21 '12 at 21:11
  • I'm assuming it doesn't take an array of arrays, rather it takes a "list" of arrays. – Charlie G Feb 21 '12 at 22:03
  • thanks got the answer! http://stackoverflow.com/questions/9385755/why-does-my-javascript-function-accept-three-arrays-but-not-an-array-containing – j.b Feb 21 '12 at 22:06