废话不说,直接上代码
async search(deep, cur, specification, res) {//商品规格的笛卡尔值实现
if (deep >= specification.length) {
cur = cur.substring(0, cur.length - 1);
res.push(cur)
return res;
}
for (let o of specification[deep].values) {
const tmp = cur
cur += specification[deep].name + '*' + o + ' '
await this.search(deep + 1, cur, specification, res)
cur = tmp
}
}
调用示例
let cstandards=[];
let specification=: [//商品规格
{
"name": "苹果果径",
"values": [
"10cm",
"20cm"
]
},
{
"name": "苹果重量",
"values": [
"5kg",
"10kg"
]
}
];
await this.search(0, '', specification, cstandards);
console.log('结果集',cstandards);