0

I am trying to make possible cartesian combination from an array of arrays but got stuck at one point. I have an array something like this:

0: ['Blue']
1: ['Small']

    var result = values.reduce((a, b) => a.reduce((r, v) => r.concat(b.map(w => [].concat(v, w))), []));
    var cartesianProduct = result.map(a => a.join('-'));

with the help of map reduce I am getting the result as: 0: "Blue-Small" which is not wrong. but what I want is something like this:

0: ['Blue-Small']
1: ['Small-Blue']

and it should be dynamic. It means if I have an array something like this:

0: ['Blue']
1: ['Small']
2: ['New']

then it should return something like this:

0: ['Blue-Small-New']
1: ['Small-Blue-New']
2: ['New-Small-Blue']
3: ['Blue-New-Small']
4: ['Small-New-Blue']
5: ['New-Blue-Small']

How is it possible to get the result like this

H John
  • 1
  • 4
  • Does this answer your question? [Finding All Combinations (Cartesian product) of JavaScript array values](https://stackoverflow.com/questions/4331092/finding-all-combinations-cartesian-product-of-javascript-array-values) – pilchard Nov 12 '21 at 11:39
  • @pilchard it will give me the same answer which I am getting through map reduce. – H John Nov 12 '21 at 11:46
  • Does this answer your question? [Permutations in JavaScript?](https://stackoverflow.com/questions/9960908/permutations-in-javascript) – nima Nov 12 '21 at 13:16
  • I tried an easy for this and it resolved my question. I did this using an array but without making any combination. I then validating the same in the back end – H John Nov 15 '21 at 10:37

0 Answers0