0

I take this step by step to avoid some missunderstanding.

Introduction:

let's say I have two arrays.

var arr1= ['a', 'b', 'c'];
var arr2= ['d', 'e', 'f'];

and i need all combination from this two arrays with just single rule and that is that combination should be just between arrays never inside of array. For my example above ouput should be:

ad, ae, af, bd, be, bf, cd, ce, cf

Example code:

I tested code like this which provide me expecting result

var combinations = [];
arr1.forEach(function(a1){
  arr2.forEach(function(a2){
    combinations.push(a1 + a2);
  });
});

This sound easy however it take me a while to figurate out. And now I face another problem which i wasnt able to solve after longer time.

Number of arrays is generated by user. So in this simple example of two arrays code above will work but if user say he want to combinate 3 arrays? or even more 10 arrays?

Question is:

How to dynamicly combinate all arrays user provide?

Andurit
  • 5,612
  • 14
  • 69
  • 121
  • How exactly user provides the arrays? – Teemu Aug 18 '16 at 14:17
  • He provide just some ID of array, JS parse his input and use stored. So he does not specify array it self – Andurit Aug 18 '16 at 14:18
  • What does the expected result look like when there are 3 arrays provided by users? – Lewis Aug 18 '16 at 14:23
  • 1
    Please use the search: [`[javascript] arrays combinations`](http://stackoverflow.com/search?q=%5Bjavascript%5D+arrays+combinations), [`[javascript] arrays cartesian product`](http://stackoverflow.com/search?q=%5Bjavascript%5D+arrays+cartesian+product) – Felix Kling Aug 18 '16 at 14:25

5 Answers5

0

Using JQuery, extend will do the trick.

Dave
  • 4,546
  • 2
  • 38
  • 59
0

Try using a two dimensional array or an array list. You find out how many arrays you are going to have (user input or a counter) and create an array of arrays that long.

Then loop through each array in the array-of-arrays, and with each of those arrays loop through every array after that one in the array-of-arrays and run the more or less same combinations.push

Luke
  • 838
  • 7
  • 17
0

I would use lodash's zip method for this

https://lodash.com/docs#zip

adam-beck
  • 5,659
  • 5
  • 20
  • 34
0

You could use an iterative and recursive approach for variable length of parts and their length.

function combine(array) {
    function c(part, index) {
        array[index].forEach(function (a) {
            var p = part.concat(a);
            if (p.length === array.length) {
                r.push(p.join(''));
                return;
            }
            c(p, index + 1);
        });
    }

    var r = [];

    c([], 0);
    return r;
}

var result = combine([['a', 'b', 'c'], ['1', '2', '3', '4'], ['A', 'B']]);

document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

With a simple map reduce:

'use strict';

let arr1= ['a', 'b', 'c'],
    arr2= ['d', 'e', 'f'];

let r = arr1.map((e) => {
    return arr2.map((f) => {
        return e + f;
    });
}).reduce((a, b) => {
    return a.concat(b);
});

console.log(r);
//Print [ 'ad', 'ae', 'af', 'bd', 'be', 'bf', 'cd', 'ce', 'cf' ]
DrakaSAN
  • 7,673
  • 7
  • 52
  • 94