dbd-meeting-html/src/utils/treeutil.js
2024-01-23 17:05:40 +08:00

21 lines
893 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* 构造树型结构数据
* @param {*} source 数据源
* @param {*} id id字段 默认 'id'
* @param {*} parentId 父节点字段 默认 'parentId'
* @param {*} children 孩子节点字段 默认 'children'
* @param {*} rootId 根Id 默认 0
*/
export function treeData (source, id, parentId, children, rootId) {
id = id || 'id'
parentId = parentId || 'parentId'
children = children || 'children'
rootId = rootId || 0
const cloneData = JSON.parse(JSON.stringify(source))// 对源数据深度克隆
return cloneData.filter(father => {
const branchArr = cloneData.filter(child => father[id] === child[parentId])// 返回每一项的子级数组
branchArr.length > 0 ? father[children] = branchArr : delete father[children]// 如果存在子级则给父级添加一个children属性并赋值
return father[parentId] === rootId // 返回第一层
})
}