jy-array-and-tree
v1.0.0
Published
```shell npm i jy-array-and-tree --save ```
Downloads
1
Readme
安装
npm i jy-array-and-tree --save
引入方式
import { JyArrayAndTree } from 'jy-throttle'
功能说明
数组和树之间的转换
1.树转数组
2.数组转树
3.合并数列表
使用示例
const X = new JyArrayAndTree()
// 数组转树
const arr = [
{ name: 'node', id: 0, pid: null},
{ name: 'node-1', id: 1, pid: 0},
{ name: 'node-11', id: 11, pid: 1},
{ name: 'node-12', id: 12, pid: 1},
{ name: 'node-2', id: 2, pid: 0},
{ name: 'node-21', id: 21, pid: 2},
{ name: 'node-211', id: 211, pid: 21},
{ name: 'node-22', id: 22, pid: 2}
]
console.log('a.arrToTree({arr})', X.arrToTree({arr}))
// 树转数组
const tree = {
"name": "node",
"id": 0,
"pid": null,
"children": [
{
"name": "node-1",
"id": 1,
"pid": 0,
"children": [
{
"name": "node-11",
"id": 11,
"pid": 1,
"children": []
},
{
"name": "node-12",
"id": 12,
"pid": 1,
"children": []
}
]
},
{
"name": "node-2",
"id": 2,
"pid": 0,
"children": [
{
"name": "node-21",
"id": 21,
"pid": 2,
"children": [
{
"name": "node-211",
"id": 211,
"pid": 21,
"children": []
}
]
},
{
"name": "node-22",
"id": 22,
"pid": 2,
"children": []
}
]
}
]
}
console.log('a.treeToArr({tree})', X.treeToArr({tree}))
// 合并数列表
const treeList = [
{
name: 'root-1',
id: 'root-1',
pid: 0,
isCheck: 1,
children: [
{
name: 'systemA',
id: 'systemA',
pid: 'root-1',
isCheck: 1,
children: [
{
name: 'systemA-menu-1',
id: 'systemA-menu-1',
pid: 'systemA',
isCheck: 1,
children: [
{
name: 'systemA-menu-btn-1',
id: 'systemA-menu-btn-1',
pid: 'systemA-menu-1',
isCheck: 1,
children: [],
},
{
name: 'systemA-menu-btn-2',
id: 'systemA-menu-btn-2',
pid: 'systemA-menu-1',
isCheck: 1,
children: [],
},
],
},
{
name: 'systemA-menu-2',
id: 'systemA-menu-2',
pid: 'systemA',
isCheck: 0,
children: [],
},
],
},
{
name: 'systemB',
id: 'systemB',
pid: 'root-1',
isCheck: 0,
children: [],
}
],
},
{
name: 'root-2',
id: 'root-2',
pid: 0,
isCheck: 1,
children: [
{
name: 'systemA',
id: 'systemA',
pid: 'root-2',
isCheck: 1,
children: [
{
name: 'systemA-menu-3',
id: 'systemA-menu-3',
pid: 'systemA',
isCheck: 1,
children: [],
},
],
},
{
name: 'systemB',
id: 'systemB',
pid: 'root-2',
isCheck: 1,
children: [
{
name: 'systemB-menu-1',
id: 'systemB-menu-1',
pid: 'systemB',
isCheck: 1,
children: [],
},
{
name: 'systemB-menu-2',
id: 'systemB-menu-2',
pid: 'systemB',
isCheck: 0,
children: [],
},
],
}
],
},
{
name: 'root-3',
id: 'root-3',
pid: 0,
isCheck: 1,
children: [
{
name: 'systemA',
id: 'systemA',
pid: 'root-3',
isCheck: 0,
children: [],
},
{
name: 'systemB',
id: 'systemB',
pid: 'root-3',
isCheck: 1,
children: [
{
name: 'systemB-menu-1',
id: 'systemB-menu-1',
pid: 'systemB',
isCheck: 1,
children: [],
},
{
name: 'systemB-menu-2',
id: 'systemB-menu-2',
pid: 'systemB',
isCheck: 0,
children: [],
},
],
},
{
name: 'systemC',
id: 'systemC',
pid: 'root-3',
isCheck: 0,
children: [],
},
],
},
{
name: 'root-4',
id: 'root-4',
pid: 0,
isCheck: 1,
children: [
{
name: 'systemD',
id: 'systemD',
pid: 'root-4',
isCheck: 1,
children: [
{
name: 'systemD-menu-1',
id: 'systemD-menu-1',
pid: 'systemD',
isCheck: 1,
children: [],
},
{
name: 'systemD-menu-2',
id: 'systemD-menu-2',
pid: 'systemD',
isCheck: 1,
children: [],
},
],
},
],
},
]
console.log(X.mergeTreeList({
treeList: treeList,
filedLabel: 'isCheck',
filedValue: 0
}))