-1

I have an associative array and I need to get all possible combinations of the first dimension.

My array looks like this:

var array = [
First:[[1,2,5,15,20], [3,4,6], [7,8]],
Second:[[2,4,6], [1,31,7]],
Third:[[1,2,6], [3,1,23,7,4], [5,8,9], [10,12,17]];

The output should look like that

array([[1,2,5,15,20],[2,4,6],[1,2,6]],
[[1,2,5,15,20],[2,4,6],[3,1,23,7,4]],
[[1,2,5,15,20],[2,4,6],[5,8,9],
[[1,2,5,15,20],[2,4,6],[10,12,17]],
[[1,2,5,15,20],[1,31,7],[1,2,6]],
...
[[7,8],[1,31,7],[10,12,17]]);

I've already tried several functions for Cartesian Products, but no one did work

Carle B. Navy
  • 1,156
  • 10
  • 28
  • 1
    Your structure is invalid.. Arrays are not key value pairs, it's simply an ordered list. You want an array of objects, each object with a key, each value of that key is a 2D array. – tymeJV Dec 04 '15 at 15:55
  • @tymeJV i think it just to clarify the purpose of this question –  Dec 04 '15 at 15:57
  • 1
    @error -- Thought that at first, but then I read the *associative array* part of the question - which makes me think otherwise. – tymeJV Dec 04 '15 at 15:57
  • @tymeJV Yeap possible you are right ! –  Dec 04 '15 at 16:00
  • 1
    Yeah, `array` would actually need to be an object to do it the way he is asking: `var object = {First:[...], Second:[...], Third:[...]};`. Or, alternately, an actual array (i.e., with no keys): `var array = [[...], [...], [...]];` – talemyn Dec 04 '15 at 16:01
  • I think I have an object like thisvar object = {First:[[...],[...],[...]], Second:[[...],[...]], Third:[[...],[...],[...]]}; – Carle B. Navy Dec 04 '15 at 16:05
  • is your array has exatcly three rows or not? – Azad Dec 04 '15 at 16:15
  • Possible duplicate of [Dynamic nested for loops to be solved with recursion](http://stackoverflow.com/questions/26703700/dynamic-nested-for-loops-to-be-solved-with-recursion) – Dave Dec 04 '15 at 18:46
  • @Azzi pretty much everything here is mutable. There can be several rows, several elements in the 2D array and several elements in the 3D array – Carle B. Navy Dec 04 '15 at 20:05

2 Answers2

1

First you would turn the object into an array. The order of properties in an object is not specified, and different browsers actually return them in different order if you loop through them. If you always have three properties, that is simple:

var arr = [ obj.First, obj.Second, obj.Third ];

Then you can create the product from the array, for example using a recursive function:

function getAll(arr, index) {
  var result = [];
  if (index == arr.length - 1) {
    for (var i = 0; i < arr[index].length; i++) {
      result.push([ arr[index][i] ]);
    }
  } else {
    var next = getAll(arr, index + 1);
    for (var i = 0; i < arr[index].length; i++) {
      for (var j = 0; j < next.length; j++) {
        result.push([ arr[index][i] ].concat(next[j]));
      }
    }
  }
  return result;
}

var obj = {
  First: [[1,2,5,15,20], [3,4,6], [7,8]],
  Second: [[2,4,6], [1,31,7]],
  Third: [[1,2,6], [3,1,23,7,4], [5,8,9], [10,12,17]]
};

var arr = [ obj.First, obj.Second, obj.Third ];

var result = getAll(arr, 0);

// Show result in snippet
document.write(JSON.stringify(result));
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • What if I have more than 3 properties and dont know their names ? – Carle B. Navy Dec 04 '15 at 20:13
  • @woulduracler: Then you have a problem. You can loop over the properties but you don't know in what order you will get them. You have to specify somehow in what order you want them. – Guffa Dec 04 '15 at 23:06
  • I managed to get the array in the form it needed to be. worked great for me! thank you – Carle B. Navy Dec 07 '15 at 13:15
0

if you have exactly 3 rows in your array then you can do it by 3 for loops;

function arrayManupulation(arr){
  // the arr must have three sub arrays
  var myarr = [];

    for (var i = 0; i < arr[0].length; i++)     
        for (var j = 0; j < arr[1].length; j++)         
            for (var k = 0; k < arr[2].length; k++)
                myarr.push([arr[0][i], arr[1][j], arr[0][k]]);

    console.log(myarr);
    return myarr;
}
Azad
  • 5,144
  • 4
  • 28
  • 56