temp2 - дерево вариантов.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //
  2. let testArr = [
  3. ["USA", "Mexico"],
  4. ["Green", "Red", "Blue"],
  5. ["Metall", "Glass", "Plastic"],
  6. ];
  7. function arrayOfTree(arr = []) {
  8. let iter = arr.reduce((a, b) => a * b.length, 1); // 18
  9. let res = Array.from(Array(iter), () => Array(arr.length)); // 18*3
  10. let step = iter;
  11. let i = 0;
  12. for (let k = 0; k < arr.length; k++) {
  13. step = step / arr[k].length; // 9, 3, 1
  14. // console.log("step - ", step);
  15. i = 0;
  16. for (let j = 0; j < iter; j++) {
  17. res[j][k] = arr[k][Math.floor(i++ / step) % arr[k].length];
  18. }
  19. }
  20. return res;
  21. }
  22. console.log(arrayOfTree(testArr));
  23. // не боится пустого массива на входе
  24. //////////////////
  25. //////////////////
  26. //////////////////
  27. //////////////////
  28. //////////////////
  29. //////////////////
  30. var list = [
  31. ["USA", "Mexico"],
  32. ["Green", "Red", "Blue"],
  33. ["Metall", "Glass", "Plastic"],
  34. ];
  35. var tree = [];
  36. list.forEach(function (listArrayValue, listKey) {
  37. let secondListKeyElement = 1;
  38. if (listKey === 0) {
  39. addElementsToSecondBranch(list, tree, secondListKeyElement, listArrayValue);
  40. }
  41. });
  42. function addElementsToSecondBranch(list, tree, secondListKeyElement, listArrayValue) {
  43. listArrayValue.forEach(function (branchKey) {
  44. tree[branchKey] = [];
  45. if (typeof tree[branchKey] !== "undefined" && typeof list[secondListKeyElement] !== "undefined") {
  46. addElementsToSecondBranch(list, tree[branchKey], secondListKeyElement + 1, list[secondListKeyElement]);
  47. }
  48. });
  49. }
  50. console.log(tree);
  51. /////////////
  52. /////////////
  53. /////////////
  54. /////////////
  55. /////////////
  56. /////////////
  57. /////////////
  58. /////////////
  59. let testArr = [
  60. ["USA", "Mexico"],
  61. ["Green", "Red", "Blue"],
  62. ["Metall", "Glass", "Plastic"],
  63. ];
  64. (() => {
  65. const inc = (indexes) => {
  66. let i;
  67. for (i = testArr.length - 1; i >= 0 && testArr[i].length <= (indexes[i] || 0) + 1; i--);
  68. if (i === -1) return NaN;
  69. indexes[i] = (indexes[i] || 0) + 1;
  70. i < testArr.length - 1 && (indexes[i + 1] = 0);
  71. return [...indexes];
  72. };
  73. let indexes = [0, 0, 0],
  74. result = [];
  75. do {
  76. result.push(indexes.map((i, j) => testArr[j][i || 0]));
  77. } while ((indexes = inc(indexes)));
  78. return result;
  79. })();
  80. for (let el of notes) {
  81. let d = document.createelement("div");
  82. body.append(d);
  83. d.append(el.text);
  84. d.classList.add("uk-table");
  85. }