flat-trees
v1.1.0
Published
flat array translate to tree data,以递归的方式把扁平数组转换成树形结构
Downloads
2
Readme
Flat Array To Trees
以递归的方式把扁平化的数组转成树形结构,可以自定义子节点的属性名
Install
npm install flat-trees
usage
import flatTrees from 'flat-trees'
const depts = [
{
id: '604e21feb205b95968e13129',
pid: '',
name: '总裁办',
code: '1001',
manager: '管理员',
introduce: '公司战略部',
createTime: '2021-03-14 22:47:25'
},
{
id: '604e2251b205b95968e1312c',
pid: '',
name: '财务部',
code: 'CWB',
manager: '管理员',
introduce: '财务部',
createTime: '2021-03-14 22:47:25'
},
{
id: '604e2262b205b95968e1312d',
pid: '604e2251b205b95968e1312c',
name: '财务核算部',
code: 'CWHSB',
manager: '管理员',
introduce: '财务核算部',
createTime: '2021-03-14 22:47:25'
},
{
id: '604e227db205b95968e1312e',
pid: '604e2251b205b95968e1312c',
name: '税务管理部',
code: 'SWGLN',
manager: '管理员',
introduce: '税务管理部',
createTime: '2021-03-14 22:47:25'
},
{
id: '604e2297b205b95968e1312f',
pid: '604e2251b205b95968e1312c',
name: '薪资管理部',
code: 'XZGLB',
manager: '管理员',
introduce: '薪资管理部',
createTime: '2021-03-14 22:47:25'
}
]
// 参数1:扁平化数据源
// 参数2:根节点初始值,默认为''
// 参数3:配置对象 id为当前数据的唯一值,pid父节点的id,children子节点属性名
// 默认值为 {id: 'id', pid: 'pid', children: 'children'}
const treeData = flatTrees(depts, '', {id: 'id', pid: 'pid', children: 'children'})
console.log(treeData)
Keywords
Flat Array , Trees Data, Recursion