M
M
MaxYenot2019-08-15 20:51:47
JavaScript
MaxYenot, 2019-08-15 20:51:47

How to get all possible combinations from several arrays with numbers?

var groups = [
["1", "4", "5", "7"]
,["3", "5", "6", "9"]
]

The output should be
13,15,16,19, 43,45,46,49, 53,55,56,59, 73,75,76,79
How to do if there are more than 2 groups? For example, up to 8?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Adamos, 2019-08-15
@Adamos

algolist.ru/maths/combinat/index.php

A
Aves, 2019-08-16
@Aves

function combine([list, ...rest]) {
  if (rest.length === 0)
    return list;
    
  const res = [];
  for (const a of list)
    for (const b of combine(rest))
      res.push(`${a}${b}`);
  
  return res;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question