4

Lets say I have several sets of options in Javascript

var color  =  ["red", "blue", "green","yellow"];
var size   =  ["small", "medium", "large"];
var weight =  ["heavy", "light"];

what is an efficient algorithm to get all the combinations of these options in an array that looks like this

["red and small and heavy", "red and small and light", "red and medium and heavy" ...]

Here's the caveat though

This function must be able to take any number of sets of options

I have a feeling that the proper way to do this is through some sort of tree traversal, but its too early to have fully thought this through and I haven't had my coffee yet

Marcin
  • 48,559
  • 18
  • 128
  • 201
Greg Guida
  • 7,302
  • 4
  • 30
  • 40

4 Answers4

8
function permutations(choices, callback, prefix) {
    if(!choices.length) {
        return callback(prefix);
    }
    for(var c = 0; c < choices[0].length; c++) {
        permutations(choices.slice(1), callback, (prefix || []).concat(choices[0][c]));
    }
}

var color  =  ["red", "blue", "green","yellow"];
var size   =  ["small", "medium", "large"];
var weight =  ["heavy", "light"];

permutations([color, size, weight], console.log.bind(console));

Seems to work...

[ 'red', 'small', 'heavy' ]
[ 'red', 'small', 'light' ]
[ 'red', 'medium', 'heavy' ]
[ 'red', 'medium', 'light' ]
[ 'red', 'large', 'heavy' ]
[ 'red', 'large', 'light' ]
[ 'blue', 'small', 'heavy' ]
[ 'blue', 'small', 'light' ]
[ 'blue', 'medium', 'heavy' ]
[ 'blue', 'medium', 'light' ]
[ 'blue', 'large', 'heavy' ]
[ 'blue', 'large', 'light' ]
[ 'green', 'small', 'heavy' ]
[ 'green', 'small', 'light' ]
[ 'green', 'medium', 'heavy' ]
[ 'green', 'medium', 'light' ]
[ 'green', 'large', 'heavy' ]
[ 'green', 'large', 'light' ]
[ 'yellow', 'small', 'heavy' ]
[ 'yellow', 'small', 'light' ]
[ 'yellow', 'medium', 'heavy' ]
[ 'yellow', 'medium', 'light' ]
[ 'yellow', 'large', 'heavy' ]
[ 'yellow', 'large', 'light' ]
AKX
  • 152,115
  • 15
  • 115
  • 172
  • 1
    doesn't work in the chrome debugger unless you wrap console.log in an anonymous function. either way awesome job! – Greg Guida Jan 11 '12 at 16:50
  • Sorry, yeah. I tested this in node.js 0.6.x, not the browser. Forgot to mention that in the post. – AKX Jan 12 '12 at 08:08
  • The console.log function mentioned in #1 above should be as: function log(message){ if(typeof console == "object"){ console.log(message); } } Then, change the call to the function to: combinations([color, size, weight], log); – Meetai.com Oct 07 '12 at 03:07
  • 'bind' is your friend: combinations([color, size, weight], console.log.bind(console)); – zaphod1984 Jun 09 '15 at 21:23
3

That would be the cartesian product of those sets: http://en.wikipedia.org/wiki/Cartesian_product

Also see: https://stackoverflow.com/questions/4796678/javascript-golf-cartesian-product

Community
  • 1
  • 1
Marcin
  • 48,559
  • 18
  • 128
  • 201
1

Tree traversal is the way to go, well recursion to be exact.

The working principle is, at each depth you would iterate through all the options for that depth, in your case the options for a list. When you choose element form last depth, you have one full set.

Shamim Hafiz - MSFT
  • 21,454
  • 43
  • 116
  • 176
-1

The console.log function mentioned in #1 above should be as:

function log(message){
    if(typeof console == "object"){
        console.log(message);
    }
}

Then, change the call to the function to:

combinations([color, size, weight], log);
Meetai.com
  • 6,622
  • 3
  • 31
  • 38