0

I have three arrays and I need to create a set of rules based on these three arrays, but I'm struggling with the logic of how to write a function that will give me every possible combination of every entry in each array. So, I have, for example:

var array 1 = [1, 2];
var array 2 = [3, 4, 5];
var array 4 = [6, 7, 8, 9, 10];

And I'd wan't get back a string, object etc of all possible combinations (which I wont attempt to work out here). So for example:

var result = ["1-3-6", "2-3-6", "1,4,6"];

And so on, so far I've tried sitting down and composing a For Loop but I'm just really not sure where to start. I also looked at maps, but could not find any examples that went this deep, so I wasn't sure if a map would get the job done either.

The actual data I want to load in, the first array has 2 entries, the second have 7 and the last one had 6, so for the workings out I've done there should be 84 entries. That was based on (Array 3 * Array 2) * Array 1.

Hope that all makes sense I know it's a bit confusing. Also worth mentioning that I'm using Angular JS so an angular solution or vanilla JS solution is preferred but not essential.

Web Develop Wolf
  • 5,996
  • 12
  • 52
  • 101
  • If I'm guessing right you expect an output array with 84 entries all strings like the example, and the strings should be formed using numbers from the arrays corresponding to their positions. – Dan Philip Bejoy Sep 07 '17 at 11:04
  • 1
    Possible duplicate of [Cartesian product of multiple arrays in JavaScript](https://stackoverflow.com/questions/12303989/cartesian-product-of-multiple-arrays-in-javascript) – Hassan Imam Sep 07 '17 at 11:16

3 Answers3

1

What you are looking is the Cartesian product of arrays. You can use a function like this (extracted from here):

function cartesian() {
    var r = [], arg = arguments, max = arg.length-1;
    function helper(arr, i) {
        for (var j=0, l=arg[i].length; j<l; j++) {
            var a = arr.slice(0); // clone arr
            a.push(arg[i][j]);
            if (i==max)
                r.push(a);
            else
                helper(a, i+1);
        }
    }
    helper([], 0);
    return r;
}

There are lot of examples, like:

JavaScript - Generating combinations from n arrays with m elements

With recursive:

Finding All Combinations of JavaScript array values

Cartesian product of multiple arrays in JavaScript

And with multiple (N) arrays:

Combine 2 arrays into 1 in all possible ways in JavaScript

Hope it helps!

JP. Aulet
  • 4,375
  • 4
  • 26
  • 39
1

Nested for loops will do

function arrComb(arr1, arr2, arr3) {
  var l1 = arr1.length,
    l2 = arr2.length,
    l3 = arr3.length,
    i, j, k, res = [];

  for (i = 0; i < l1; i++) {
    for (j = 0; j < l2; j++) {
      for (k = 0; k < l3; k++) {
        res.push(arr1[i] + '-' + arr2[j] + '-' + arr3[k]);
      }
    }
  }
  console.log(res)
}

arrComb([1, 2], [3, 4, 5], [6, 7, 8, 9, 10]);
Dan Philip Bejoy
  • 4,321
  • 1
  • 18
  • 33
  • There is no need to cache an arrays length because it's a property that gets updated every time you add an element. It's not a method that counts the elements every time. – Anis Alibegić Sep 07 '17 at 11:14
  • I could but it rarely makes a difference. The whole snippet is supposed to be a function to be invoked. Did it this way just to demonstrate. – Dan Philip Bejoy Sep 07 '17 at 11:14
1

A bit more elegant:

var array_1 = [1, 2];
var array_2 = [3, 4, 5];
var array_4 = [6, 7, 8, 9, 10];
var result = [];

for (var a1 of array_1)
{
    for (var a2 of array_2)
    {
        for (var a3 of array_4)
        {
            result.push("\""+a1+"-"+a2+"-"+a3+"\"")
        }
    }
}
alert("["+result+"]")
Hezi Shahmoon
  • 388
  • 1
  • 7