/**
* 将数据序列化为树形结构
* @param {Array} list 树结构数据
* @param {String} id 用于通用遍历树,每种树结构的id取名可能不一样
* @param {Srting} name 用于通用遍历树,每种树结构的pid取名可能不一样
* @param {Srting} name 用于通用遍历树,每种树结构的name取名可能不一样
* @return {[type]} [description]
*/
function serializeTree(list = [], options = {}) {
const { id = 'funcId', parentId = 'funcParent', name = 'funcName' } = options;
const trees = []; // 树状结构
const maps = {}; // 节点id与节点对象映射
list = list.map((item) => {
return { ...item };
});
let pid; // 父节点id
// 遍历添加映射
list.forEach((item) => {
// 设置转换为通用结构
item.id = item[id];
item.name = item[name];
item.parentId = item[parentId];
item.label = item.name;
item.title = item.name;
item.key = item.id;
item.value = item.id;
// 映射结构
maps[item.id] = item;
});
/
js将平铺展开的数据树化
最新推荐文章于 2023-11-27 14:49:19 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)