方法
/**
* @description tree转数组
* @param { array } treeList - 树结构数据
* @param { string|number } pKey - 父级的唯一标识
* @returns { Array } 普通一维数组
* */
function treeToArr(treeList, pKey) {
return treeList.reduce((arr, { key, title, type, typeName, children = [] }) => {
return arr.concat([{ key, title, type, typeName, pKey }], treeToArr(children, key))
}, [])
}
demo
dataSource:
const treeData = [{"key":"1530855447689961473","title":"xx厂","scopedSlots":{"title":"custom"},"haveOperate":false,"type":"FACTORY","typeName":"厂","children":[{"key":"1530855803517935617","title":"电镀车间","scopedSlots":{"title":"custom"},"haveOperate":false,"type":"WORKPLACE","typeName":"车间","children":[{"key":"1530856850642706434","title":"电镀工序","scopedSlots":{"title":"custom"},"haveOperate":false,"type":"PROCESS","typeName":"工序","children":[{"key":"1530856914496790529","title":"电镀设备","scopedSlots":{"title":"custom"},"haveOperate":false,"type":"DEVICE","typeName":"设备","haveWatch":true}],"haveWatch":false}],"haveWatch":true},{"key":"1530856231341137922","title":"冷却车间","scopedSlots":{"title":"custom"},"haveOperate":false,"type":"WORKPLACE","typeName":"车间","children":[{"key":"1530857255871193090","title":"冷却工序","scopedSlots":{"title":"custom"},"haveOperate":false,"type":"PROCESS","typeName":"工序","children":[{"key":"1530857407587557377","title":"冷却设备","scopedSlots":{"title":"custom"},"haveOperate":false,"type":"DEVICE","typeName":"设备","haveWatch":true}],"haveWatch":false}],"haveWatch":true},{"key":"1530856558362632193","title":"融化车间","scopedSlots":{"title":"custom"},"haveOperate":false,"type":"WORKPLACE","typeName":"车间","children":[{"key":"1530857575770759170","title":"融化工序","scopedSlots":{"title":"custom"},"haveOperate":false,"type":"PROCESS","typeName":"工序","children":[{"key":"1530857682977169410","title":"融化设备","scopedSlots":{"title":"custom"},"haveOperate":false,"type":"DEVICE","typeName":"设备","haveWatch":true}],"haveWatch":false}],"haveWatch":true},{"key":"1530856762038034433","title":"安装车间","scopedSlots":{"title":"custom"},"haveOperate":false,"type":"WORKPLACE","typeName":"车间","children":[{"key":"1530857795439042561","title":"安装工序","scopedSlots":{"title":"custom"},"haveOperate":false,"type":"PROCESS","typeName":"工序","children":[{"key":"1530857954679988225","title":"安装设备","scopedSlots":{"title":"custom"},"haveOperate":false,"type":"DEVICE","typeName":"设备","haveWatch":true}],"haveWatch":true}],"haveWatch":true}],"haveWatch":true}]
use:
treeToArr(data, 0)
注意(notice):
key, title, type, typeName
这几个参数为数据中的业务参数,实际项目里面需要什么就改成什么
评论区