0

Let's just say I have an array with n number of index:

0:{prop1: 'abc', prop2: ['1', '2', '3'],
1:{prop1: 'xyz', prop2: ['8', '6']

Now, what I am trying to make is a combination like this:

(1,8)
(1,6)
(2,8)
(2,6)
(3.8)
(3,6)

I have tried multiple methods couldn't achieve this combination

H John
  • 1
  • 4

1 Answers1

0

Check CartesianProduct class from package js-combinatorics, I think this is the right tool for your task:

let it = new CartesianProduct('123','86');
it.length;  // 6
[...it];    /* [
  ['1', '8'],
  ['2', '8'],
  ['3', '8'],
  ['1', '6'],
  ['2', '6'],
  ['3', '6']
] */
Maksym Shcherban
  • 742
  • 4
  • 13
  • instead of writing a few strings of code you prefer loading some lib? – Aliaksandr Pitkevich Oct 14 '21 at 13:32
  • Yes, I am just like the guy from JS meme who imports `is-odd` NPM package instead of simply writing `x % 2` :D Just kidding. But in this case, I think using a tested library code for combinatorial functions is reasonable. – Maksym Shcherban Oct 14 '21 at 13:52