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

1
  • thanks for that formatting, Kolink Commented Feb 21, 2012 at 19:54

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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
I'm assuming it doesn't take an array of arrays, rather it takes a "list" of arrays.
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/

2 Comments

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'])].
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?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.