1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- export function handleTree(data, id, parentId, children, rootId) {
- id = id || 'id'
- parentId = parentId || 'parentId'
- children = children || 'children'
- rootId = rootId || Math.min.apply(Math, data.map(item => {
- return item[parentId]
- })) || 0
-
- const cloneData = JSON.parse(JSON.stringify(data))
-
- const treeData = cloneData.filter(father => {
- let branchArr = cloneData.filter(child => {
-
- return father[id] === child[parentId]
- });
- branchArr.length > 0 ? father.children = branchArr : '';
-
- return father[parentId] === rootId;
- });
- return treeData !== '' ? treeData : data;
- }
- export function convertTree(data) {
-
- const cloneData = JSON.parse(JSON.stringify(data))
-
- for (let first = 0; first < cloneData.length; first++) {
- for (let second = 0; second < cloneData[first].children.length; second++) {
- for (let three = 0; three < cloneData[first].children[second].children.length; three++) {
- if (data[first].children[second].children[three].children == undefined ||
- data[first].children[second].children[three].children === 0) {
- data[first].children[second].children.splice(second, 1);
- }
- }
- if (data[first].children[second].children == undefined || data[first].children[second].children === 0) {
- data[first].children.splice(second, 1);
- }
- }
- if (data[first].children == undefined || data[first].children.length === 0) {
- data.splice(first, 1);
- }
- }
- return data;
- }
|