3

I have the following array in js:

var list = [
  ['nice', 'delicious', 'red'],
  ['big', 'tiny'],
  ['apple']
];

I'd like to get all the possible variations, like:

['nice', 'big', 'apple']
['delicious', 'big', 'apple']
['red', 'big', 'apple']
['nice', 'tiny', 'apple']
...

What is the best / most eloquent way to achieve this?

jpapp
  • 39
  • 2
  • What can be length of possible variation? What if two different arrays have same value can it be duplicated in possible variation? – Viktor Kukurba Jan 25 '16 at 22:23
  • If the two arrays have the same value, you can duplicate it in the end results. At the end I just need an array with all possible variations from the original array. – jpapp Jan 25 '16 at 22:30

2 Answers2

6

I was trying to come up with something really fancy like recursion or stacks of maps and reduces, but this questions seems not as complicated enough to justify something different than:

var result = list[0].map(function(item) { return [item]; });

for (var k = 1; k < list.length; k++) {
    var next = [];
    result.forEach(function(item) {
        list[k].forEach(function(word) {
            var line = item.slice(0);
            line.push(word);
            next.push(line);
        })
    });
    result = next;
}

console.log(result);
Kuba Wyrostek
  • 6,163
  • 1
  • 22
  • 40
-1

EDIT: This did not answer the OP's question, slightly different topic. This should be the right one.

Finding All Combinations of JavaScript array values

I would approach this problem from a more 'general' standpoint. You have an array, and would like to get a random value from it. As long as you know the length of the array, this is easy. The way I would do it is, generate a random index from the 'length' property of the individual array and pull that out.

Here is a quick and dirty demo.

var sentence = '';
var fragments = [
  ['delicious', 'bad', 'gross'],
  ['red', 'green', 'blue'],
  ['apple', 'orange', 'basketball']
];

// This could be used over and over for any array,
// just pass it the length
function randomIndex(i) {
  return Math.floor(Math.random() * i);
}

// This is more specific to your use case, go into each
// each child array and just pick a random element
fragments.forEach(function(el) {
  possibleIndex = randomIndex(el.length);
  // Join the next piece
  sentence += el[randomIndex(possibleIndex)] + ' ';
});

// Do something with sentence here
console.log(sentence) //=> 'bad green apple'

You can clean this up of course, and would need to clear that sentence variable each time. Like I said, quick and dirty. Stuff like this should be abstracted out, and is a common use case for libraries such as underscore and lodash.

Here is a bin that shows it in action.

https://jsbin.com/guxiziwoqa/edit?html,js,output

Community
  • 1
  • 1
Dustin Stiles
  • 1,414
  • 9
  • 12
  • Thanks for the reply Dustin, but I need an array of all possible variants, not just a random one. I'm not sure if I could use your solution to get all of them. Maybe I'm too lame. :) – jpapp Jan 25 '16 at 22:40
  • Ahh yes, I see now! That questions has been answered rather elegantly in this article here http://stackoverflow.com/questions/4331092/finding-all-combinations-of-javascript-array-values – Dustin Stiles Jan 25 '16 at 22:46