generateSkuList(goods) {
const result = []
const temp = []
const length = goods.length
const dfs = (index) => {
if (index === length) {
result.push([...temp])
return
}
const { name, values } = goods[index]
for (let i = 0; i < values.length; i++) {
temp[index] = values[i]
dfs(index + 1)
}
}
dfs(0)
return result
},
const goods = [
{
name: '颜色',
values: ['红色', '蓝色']
},
{
name: '尺寸',
values: ['S', 'M', 'L']
},
{
name: '材质',
values: ['棉', '麻']
}
]
const skuList = this.generateSkuList(goods)
console.log(skuList)
生成结果
[
['红色', 'S', '棉'],
['红色', 'S', '麻'],
['红色', 'M', '棉'],
['红色', 'M', '麻'],
['红色', 'L', '棉'],
['红色', 'L', '麻'],
['蓝色', 'S', '棉'],
['蓝色', 'S', '麻'],
['蓝色', 'M', '棉'],
['蓝色', 'M', '麻'],
['蓝色', 'L', '棉'],
['蓝色', 'L', '麻']
]