crate combination from the any number lists in javascript

Here is an example code that creates all combinations of a given set of arrays using recursion and backtracking in JavaScript:

index.tsx
function getAllCombinations(arrays) {
  let result = [];
  
  function backtrack(combination, start) {
    if (combination.length === arrays.length) {
      result.push(combination);
      return;
    }
    
    for (let i = start; i < arrays[combination.length].length; i++) {
      backtrack([...combination, arrays[combination.length][i]], i + 1);
    }
  }
  
  backtrack([], 0);
  return result;
}

// Usage
let arrays = [
  [1, 2],
  [3, 4],
  [5, 6]
];
console.log(getAllCombinations(arrays)); // Output: [[1,3,5],[1,3,6],[1,4,5],[1,4,6],[2,3,5],[2,3,6],[2,4,5],[2,4,6]]
578 chars
26 lines

The getAllCombinations function takes an array of arrays as an argument and returns an array of all combinations of elements from the given arrays. The backtrack function is a recursive helper function that generates all combinations by keeping track of the current combination and the starting index of the next array to be added to the combination. The result array holds all valid combinations as they are generated.

gistlibby LogSnag