商品详情:SKU 选择的属性展示

This commit is contained in:
YunaiV
2023-12-09 20:04:51 +08:00
parent 4854cee7ca
commit 3157df8464
3 changed files with 76 additions and 85 deletions
+44
View File
@@ -156,3 +156,47 @@ function getDayjsTime(time) {
return dayjs.unix(parseInt(time));
}
}
/**
* 从商品 SKU 数组中,转换出商品属性的数组
*
* 类似结构:[{
* id: // 属性的编号
* name: // 属性的名字
* values: [{
* id: // 属性值的编号
* name: // 属性值的名字
* }]
* }]
*
* @param skus 商品 SKU 数组
*/
export function convertProductPropertyList(skus) {
let result = [];
for (const sku of skus) {
if (!sku.properties) {
continue
}
for (const property of sku.properties) {
// ① 先处理属性
let resultProperty = result.find(item => item.id === property.propertyId)
if (!resultProperty) {
resultProperty = {
id: property.propertyId,
name: property.propertyName,
values: []
}
result.push(resultProperty)
}
// ② 再处理属性值
let resultValue = resultProperty.values.find(item => item.id === property.valueId)
if (!resultValue) {
resultProperty.values.push({
id: property.valueId,
name: property.valueName
})
}
}
}
return result;
}