1. // 超简单的笛卡尔积算法
    2. const arr = [
    3. [ '高', '低'],
    4. [ '24G', '48G'],
    5. [ '红', '黄', '蓝']
    6. ]
    7. function cartesianProductOf() {
    8. return Array.prototype.reduce.call(arguments, function(a, b) {
    9. var ret = [];
    10. a.forEach(function(a) {
    11. b.forEach(function(b) {
    12. ret.push(a.concat([b]));
    13. });
    14. });
    15. return ret;
    16. }, [[]]);
    17. }
    18. console.log(JSON.stringify(cartesianProductOf(...arr)))
    19. // [
    20. // ["高","24G","红"],["高","24G","黄"],
    21. // ["高","24G","蓝"],["高","48G","红"],
    22. // ["高","48G","黄"],["高","48G","蓝"],
    23. // ["低","24G","红"],["低","24G","黄"],
    24. // ["低","24G","蓝"],["低","48G","红"],
    25. // ["低","48G","黄"],["低","48G","蓝"]
    26. // ]

    项目需要 , 升级版

    1. <script>
    2. // 笛卡尔积的实现
    3. const arr = [
    4. {
    5. "id": 35,
    6. "name": "颜色",
    7. "chooseValue": [
    8. {
    9. "id": "1145225013440352258",
    10. "text": "红"
    11. },
    12. {
    13. "id": "1145225013440352259",
    14. "text": "黄"
    15. }
    16. ]
    17. },
    18. {
    19. "id": 40,
    20. "name": "尺码",
    21. "chooseValue": [
    22. {
    23. "id": "1145225014644117506",
    24. "text": "34"
    25. },
    26. {
    27. "id": "1145225014635728898",
    28. "text": "32"
    29. }
    30. ]
    31. },
    32. {
    33. "id": 68,
    34. "name": "宽度",
    35. "chooseValue": [
    36. {
    37. "id": "1145225016594468865",
    38. "text": "dm"
    39. }
    40. ]
    41. }
    42. ]
    43. function descartes(...args) {
    44. if (args.length < 2) {
    45. return args[0] || [];
    46. }
    47. return [].reduce.call(args, (col, set) => {
    48. let res = [];
    49. if(col instanceof Array){
    50. col.forEach(c => {
    51. set.chooseValue.forEach(s => {
    52. let t = [].concat(Array.isArray(c) ? c : [{[set.id]:s,"text":set.name}]);
    53. let S = {}
    54. S[set.id] = s
    55. S.test = set.name
    56. t.push(S);
    57. res.push(t);
    58. });
    59. });
    60. }else{
    61. col.chooseValue.forEach(c => {
    62. set.chooseValue.forEach(s => {
    63. let t = [].concat(Array.isArray(c) ? c : [{[set.id]:s,"text":set.name}]);
    64. let S = {}
    65. S[col.id] = c
    66. S.test = col.name
    67. t.push(S);
    68. res.push(t);
    69. });
    70. });
    71. }
    72. return res;
    73. });
    74. }
    75. console.log(JSON.stringify(descartes(...arr)))
    76. // 输出生成的sku:
    77. // [
    78. // [
    79. // {
    80. // "40": {
    81. // "id": "1145225014644117506",
    82. // "text": "34"
    83. // },
    84. // "text": "尺码"
    85. // },
    86. // {
    87. // "35": {
    88. // "id": "1145225013440352258",
    89. // "text": "红"
    90. // },
    91. // "test": "颜色"
    92. // },
    93. // {
    94. // "68": {
    95. // "id": "1145225016594468865",
    96. // "text": "dm"
    97. // },
    98. // "test": "宽度"
    99. // }
    100. // ],
    101. // [
    102. // {
    103. // "40": {
    104. // "id": "1145225014635728898",
    105. // "text": "32"
    106. // },
    107. // "text": "尺码"
    108. // },
    109. // {
    110. // "35": {
    111. // "id": "1145225013440352258",
    112. // "text": "红"
    113. // },
    114. // "test": "颜色"
    115. // },
    116. // {
    117. // "68": {
    118. // "id": "1145225016594468865",
    119. // "text": "dm"
    120. // },
    121. // "test": "宽度"
    122. // }
    123. // ],
    124. // [
    125. // {
    126. // "40": {
    127. // "id": "1145225014644117506",
    128. // "text": "34"
    129. // },
    130. // "text": "尺码"
    131. // },
    132. // {
    133. // "35": {
    134. // "id": "1145225013440352259",
    135. // "text": "黄"
    136. // },
    137. // "test": "颜色"
    138. // },
    139. // {
    140. // "68": {
    141. // "id": "1145225016594468865",
    142. // "text": "dm"
    143. // },
    144. // "test": "宽度"
    145. // }
    146. // ],
    147. // [
    148. // {
    149. // "40": {
    150. // "id": "1145225014635728898",
    151. // "text": "32"
    152. // },
    153. // "text": "尺码"
    154. // },
    155. // {
    156. // "35": {
    157. // "id": "1145225013440352259",
    158. // "text": "黄"
    159. // },
    160. // "test": "颜色"
    161. // },
    162. // {
    163. // "68": {
    164. // "id": "1145225016594468865",
    165. // "text": "dm"
    166. // },
    167. // "test": "宽度"
    168. // }
    169. // ]
    170. // ]
    171. </script>