1

Having a hard time googling for this since I'm not sure what the concepts are called, and all of the "combinations of two arrays/groups" SO posts are not giving me the output I would expect.

Example arrays:

var array1 = ['Bob', 'Tina'];
var array2 = [0, 100];

I can find possible combinations with nested looping through both arrays. But that would give me an output like:

var array1 = ['Bob', 'Tina'];
var array2 = [0, 100];
var options = []

array1.forEach(function (name) {
  array2.forEach(function (number) {
    options.push([name, number])
  })
})


console.log(options);
> [ [ 'Bob', 0 ], [ 'Bob', 100 ], [ 'Tina', 0 ], [ 'Tina', 100 ] ]

this post (Creating Combinations in JavaScript) gives me the output above

But what I'm really looking for would give me arrangements/combinations like this:

[
  [['Bob', 0], ['Tina', 0]],
  [['Bob', 0], ['Tina', 100]],
  [['Bob', 100], ['Tina', 0]],
  [['Bob', 100], ['Tina', 100]]
]

And it would need to be able to scale with longer arrays, but 2x2 is the easiest example.

This cartesian example here (Matrix combinations of two arrays in javascript) also gave me broken out strings and not correlated arrangements:

[ { '0': 'B', '1': 'o', '2': 'b' },
  { '0': 'B', '1': 'o', '2': 'b' },
  { '0': 'T', '1': 'i', '2': 'n', '3': 'a' },
  { '0': 'T', '1': 'i', '2': 'n', '3': 'a' } ]

I have been looking through google and SO but I'm hitting roadblocks because I'm not sure what I'm actually looking for.

Community
  • 1
  • 1
Adam Bickford
  • 187
  • 1
  • 2
  • 13
  • 2
    Some form of iterating through both arrays is how you would do this, but you need to show what you have tried so far. See [ask] and [mcve] – Tibrogargan Sep 09 '16 at 18:11
  • Possible duplicate of [Creating Combinations in JavaScript](http://stackoverflow.com/questions/8822950/creating-combinations-in-javascript) – VLAZ Sep 09 '16 at 18:14
  • I recommend doing it by hand with different inputs, and looking for patterns that you can utilize. As a programmer, making at least a good attempt is in your job description. – 4castle Sep 09 '16 at 18:18
  • Edited to include more info and more links to solutions I've tried. My problem isn't just making combinations but creating possible options where I can get arrangements like `[['Bob', 0], ['Tina',0]]` – Adam Bickford Sep 09 '16 at 18:29
  • What I notice is that you need double permutations. First create a set of every possible element in the second array with each element in the first array, and they find all combinations of THOSE permutations – 4castle Sep 09 '16 at 18:32
  • Ahhhh ok, that sounds it might be it, I'll try that. – Adam Bickford Sep 09 '16 at 18:37
  • This ended up making me wish I knew more number theory :) – Tibrogargan Sep 09 '16 at 23:41

3 Answers3

1

I hope this might help you to get your actual combination:

var array1 = ['Bob', 'Tina'];
var array2 = [0, 100];
var resultArray = []

for (var i = 0; i < array1.length; i++) {
  for (var j = 0; j < array2.length; j++) {
    var tempArray = [];
    tempArray.push(array1[i]);
    tempArray.push(array2[j]);
    resultArray.push(tempArray);
  }
}

for (var i = 0; i < array2.length; i++) {
  for (var j = 0; j < array1.length; j++) {
    var tempArray = [];
    tempArray.push(array1[j]);
    tempArray.push(array2[i]);
    resultArray.push(tempArray);
  }
}

console.log(resultArray);
Samrat Alamgir
  • 355
  • 2
  • 13
0

This solves the problem, but don't know how efficient it is - I'll bet there's much better ways of doing it.

var array1 = ['Bob', 'Tina', 'Sam'];
var array2 = [0, 100, 200];

var resultCount = Math.pow(array2.length, array1.length);
var results = new Array(resultCount);
for(var i = 0; i < resultCount; i++) {
    results[i] = new Array(array1.length);
}

for(var row = 0; row < resultCount; row++) {
    for(var column = 0; column < array1.length; column++) {
        var result = row * array1.length + column;
        var category = array1.length * Math.pow( array2.length, array1.length - 1 - column);
        var idx = ~~(result / category) % array2.length;
        results[row][column] = [ array1[column], array2[idx] ];
 }
}
console.log(results);

I think you could actually do this in terms of number/string manipulation by thinking of array2 as a radix (each value maps to a digit) and array1 as the number of digits. As long as array2 has 36 or less elements you could convert every number from 0 up to the number of combinations as a string (Number.toString(radix)) and then convert each char from the resulting string back to an index into your arrays.

Tibrogargan
  • 4,508
  • 3
  • 19
  • 38
0

I'll give you some hints that you might find helpful. First, I assume you know how to iterate over an array:

var array1 = ['Bob', 'Tina'];

for (var i = 0; i < array1.length; i++) {
    // you can access the ith element  using array1[i]
     // for example:
    alert(array1[i]);
}

I also assume you know how to execute nested looping:

var array1 = ['Bob', 'Tina'];
var array2 = [0, 100];

for (var i = 0; i < array1.length; i++) {
    for (var j = 0; j < array2.length; j++) {
        alert(array1[i] + " " + array2[j]):
    }
}

What you may be missing is how to construct a new array:

var five = 5;
var six = 6;

var myArray = [five, six];

You also may be missing the ability to push a new array onto an existing "master" array:

var five = 5;
var six = 6;
var masterArray = [];

for (var i = 0; i < 10; i++) {
    masterArray.push([five, six]);
}

Hope this helps, and good luck!

Nate Vaughan
  • 3,471
  • 4
  • 29
  • 47
  • Thanks, and I have edited my post based on other feedback to include options like this that I have tried. – Adam Bickford Sep 09 '16 at 18:31
  • The nested loop + pushing options to a master array gives me possible combinations like `> [ [ 'Bob', 0 ], [ 'Bob', 100 ], [ 'Tina', 0 ], [ 'Tina', 100 ] ]` but I need to find all combinations including ones where Bob and Tina can get the same value. – Adam Bickford Sep 09 '16 at 18:34
  • 1
    The same principles are at play, but you will need 3 layers of looping. – Nate Vaughan Sep 09 '16 at 19:09