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?

4
  • How exactly user provides the arrays? Commented Aug 18, 2016 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 Commented Aug 18, 2016 at 14:18
  • What does the expected result look like when there are 3 arrays provided by users? Commented Aug 18, 2016 at 14:23
  • 1
    Please use the search: [javascript] arrays combinations, [javascript] arrays cartesian product Commented Aug 18, 2016 at 14:25

5 Answers 5

0

Using JQuery, extend will do the trick.

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

0

I would use lodash's zip method for this

https://lodash.com/docs#zip

Comments

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>');

Comments

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' ]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.