temp2.js 710 B

123456789101112131415161718192021222324252627
  1. let testArr = [
  2. ["USA", "Mexico"],
  3. ["Green", "Red", "Blue"],
  4. ["Metall", "Glass", "Plastic"],
  5. ];
  6. function arrayOfTree(arr = []) {
  7. let iter = arr.reduce((a, b) => a * b.length, 1); // 18
  8. let res = Array.from(Array(iter), () => Array(arr.length)); // 18*3
  9. let step = iter;
  10. let i = 0;
  11. for (let k = 0; k < arr.length; k++) {
  12. step = step / arr[k].length; // 9, 3, 1
  13. // console.log("step - ", step);
  14. i = 0;
  15. for (let j = 0; j < iter; j++) {
  16. res[j][k] = arr[k][Math.floor(i++ / step) % arr[k].length];
  17. }
  18. }
  19. return res;
  20. }
  21. console.log(arrayOfTree(testArr));
  22. // не боится пустого массива на входе