小程序商城通过笛卡尔积算法实现sku组合

时间:2022-05-31 13:05 来源:
```javascript mounted() { let colorArray = ['白色','红色','黑色'] //选择的颜色 let sizeArray = ['X','XL','XXL'] //选择的尺寸 let allArray =[] //组合数组 格式为:[[],[]] allArray.push(colorArray) allArray.push(sizeArray) this.cartesianProductOf.apply(this,allArray); //调用笛卡尔积方法 }, methods: { cartesianProductOf:function(){ //笛卡尔积 return Array.prototype.reduce.call(arguments,function(a, b) { var ret = []; a.forEach(function(a) { b.forEach(function(b) { ret.push(a.concat([b])); }); }); console.log(ret) //组合成功 return ret; }, [[]]); }, } ```