/* 获取商品分类 */
APGetGovCategoryTree() {
APGetGovCategoryTree().then((res) => {
// treeNames转为数组
this.Category = res.list.map((item)=>{
return {
cate_id: item.id,
categoryName: item.name,
interfaceCode: item.interfaceCode,
treeNames: item.treeNames.split('-').filter(items => items !== ''),
}
});
// 数据转为树形结构
this.CategoryList = this.Category.map(item => {
let children=[];
this.buildChildrenNode(children, item.treeNames)
return children[0];
})
// 相同级别的相同分类合并
this.changeList(this.CategoryList);
});
},
// 商品分类转为树形结构
buildChildrenNode(children, itemArr){
for (let i in itemArr) {
let node = {
value: itemArr[i],
label: itemArr[i],
}
children.push(node)
if (i != itemArr.length - 1 && !children[children.length - 1].children) {
children[children.length - 1].children = []
}
children = i == itemArr.length - 1 ? children : children[children.length - 1].children
}
},
// 相同级别的相同分类合并
changeList(data){
for(let i=0; i<data.length-1; i++){
for(let j=i+1; j<data.length; j++){
if(i!=j && data[i].label==data[j].label){
this.changeListchild(data[i].children,data[j].children);
data.splice(j,1);
j--;
}
}
}
},
changeListchild(a,b){
if(a && b){
if(a[0].label == b[0].label){
this.changeListchild(a[0].children,b[0].children);
}
if(a[0].label != b[0].label){
a.push(b[0]);
this.changeList(a);
}
}
},
05-10
2474
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)