vue通过笛卡儿积实现sku库存配置

vue通过笛卡儿积实现sku库存配置

目标

通过页面上渲染出来的动态属性 实现sku库存配置

将已选择的所有属性,通过笛卡儿积实现sku库存配置
以两个属性为例子,举例说明:
1x1:
白色
S
结合之后就是
[白色,S]

 this.selectCheckArr=[
    [
        {
            "attrId": 0,
            "attrName": "颜色",
            "attrValueId": 00,
            "attrValueName": "白色"
        }
    ],
    [
        {
            "attrId": 1,
            "attrName": "大小",
            "attrValueId": 11,
            "attrValueName": "S"
        }
    ]
]
 this.selectCheckArr = this.getProducts(this.selectCheckArr)
 console.log(this.selectCheckArr)
 /** 输出:[
    [
        {
            "attrId": 0,
            "attrName": "颜色",
            "attrValueId": 00,
            "attrValueName": "白色"
        },
        {
            "attrId": 1,
            "attrName": "大小",
            "attrValueId": 11,
            "attrValueName": "S"
        }
    ]
]
**/

1x2
白色 黄色
S
结合之后就是
[白色,S ],[黄色,S ]

 this.selectCheckArr=[
    [
        {
            "attrId": 0,
            "attrName": "颜色",
            "attrValueId": 00,
            "attrValueName": "白色"
        },
        {
            "attrId": 0,
            "attrName": "颜色",
            "attrValueId": 01,
            "attrValueName": "黄色"
        }
    ],
    [
        {
            "attrId": 1,
            "attrName": "大小",
            "attrValueId": 11,
            "attrValueName": "S"
        }
    ]
]
 this.selectCheckArr = this.getProducts(this.selectCheckArr)
 console.log(this.selectCheckArr)
 /** 输出: 
 [
    [
        {
            "attrId": 0,
            "attrName": "颜色",
            "attrValueId": 00,
            "attrValueName": "白色"
        },
        {
            "attrId": 1,
            "attrName": "大小",
            "attrValueId": 11,
            "attrValueName": "S"
        }
    ],
    [
        {
            "attrId": 0,
            "attrName": "颜色",
            "attrValueId": 01,
            "attrValueName": "黄色"
        },
       {
            "attrId": 1,
            "attrName": "大小",
            "attrValueId": 11,
            "attrValueName": "S"
        }
    ]
]
**/

2x2
白 黄
S M
结合之后就是
[白色,S ],[白色,M ],[黄色,S ],[黄色,M ]

 this.selectCheckArr= [
    [
        {
            "attrId": 0,
            "attrName": "颜色",
            "attrValueId": 00,
            "attrValueName": "白色"
        },
        {
            "attrId": 0,
            "attrName": "颜色",
            "attrValueId": 01,
            "attrValueName": "黄色"
        }
    ],
    [
        {
            "attrId": 1,
            "attrName": "大小",
            "attrValueId": 11,
            "attrValueName": "S"
        },
        {
            "attrId": 1,
            "attrName": "大小",
            "attrValueId": 12,
            "attrValueName": "M"
        }
    ]
]
 this.selectCheckArr = this.getProducts(this.selectCheckArr)
 console.log(this.selectCheckArr)
 /** 输出: 
 [
    [
        {
            "attrId": 0,
            "attrName": "颜色",
            "attrValueId": 00,
            "attrValueName": "白色"
        },
        {
            "attrId": 1,
            "attrName": "大小",
            "attrValueId": 11,
            "attrValueName": "S"
        }
    ],
    [
        {
            "attrId": 0,
            "attrName": "颜色",
            "attrValueId": 00,
            "attrValueName": "白色"
        },
        {
            "attrId": 1,
            "attrName": "大小",
            "attrValueId": 12,
            "attrValueName": "M"
        }
    ],
    [
         {
            "attrId": 0,
            "attrName": "颜色",
            "attrValueId": 01,
            "attrValueName": "黄色"
        },
         {
            "attrId": 1,
            "attrName": "大小",
            "attrValueId": 11,
            "attrValueName": "S"
        }
    ],
    [
        {
            "attrId": 0,
            "attrName": "颜色",
            "attrValueId": 01,
            "attrValueName": "黄色"
        },
       {
            "attrId": 1,
            "attrName": "大小",
            "attrValueId": 12,
            "attrValueName": "M"
        }
    ]
]
**/

笛卡儿积方法

    // 笛卡儿积
      getProducts(specs) {
        if (!specs || specs.length == 0) {
          return [];
        } else {
          return joinSpec([
            []
          ], specs, 0, specs.length - 1);
        }

        function joinSpec(prevProducts, specs, i, max) {
          var currentProducts = [],
            currentProduct, currentSpecs = specs[i];
          if (i > max) {
            return prevProducts;
          }
          prevProducts.forEach(function (prevProduct) {
            currentSpecs.forEach(function (spec) {
              currentProduct = prevProduct.slice(0);
              currentProduct.push(spec);
              currentProducts.push(currentProduct);
            });
          });
          return joinSpec(currentProducts, specs, ++i, max);
        }
      },

注意

this.getProducts()的入参,需与博主保持一致(数组对象),否则会有问题哦~
如果是1x1 ,就是总共生成1条
[ [ { } ] , [ { } ] ],变成 [ [ { } , { } ] ]
如果是2x1 ,就是总共生成2条
[ [ { } ,{ } ] , [ { } ] ] 变成 [ [ { } ,{ } ] , [ { } ,{ } ] ]

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值