要转换成树形菜单的格式,首先原数据的格式要有自身ID与上级ID如:
其中id为自身ID,pid为上级ID,默认一级列表的上级ID为0;
let treeid = 0 //初始为一级列表,默认id为0 let treedata = [] let list = { id: 1, name: "名称", pid: 0, //所要更改的数据 } changeTree(list,treeid,reedata) const changeTree = (datalist, id, newArr) => { for (let i = 0; i < datalist.length; i++) { if (datalist[i].pid == id) { newArr.push(datalist[i]); } } for (let i = 0; i < newArr.length; i++) { newArr[i].children = []; changeTree(scenicList.value, newArr[i].id, newArr[i].children); if (newArr[i].children.length == 0) { delete newArr.children; } } };