UNPKG

lei-jia-utils

Version:

55 lines (50 loc) 1.43 kB
export function hello() { console.log('hello, dujiajia') } export function deepClone(source) { if (!source && typeof source !== 'object') { throw new Error('error arguments', 'deepClone') } const targetObj = source.constructor === Array ? [] : {} Object.keys(source).forEach(keys => { if (source[keys] && typeof source[keys] === 'object') { targetObj[keys] = deepClone(source[keys]) } else { targetObj[keys] = source[keys] } }) return targetObj } export function uniqueArr(arr) { return Array.from(new Set(arr)) } export function listToTreeUper(ParentId, all) { let tree = [] let arr = all arr.forEach(item => { if (item.ParentId == ParentId) { if (listToTree(item.Id, arr).length > 0) { // 递归如果存在children设置children item.Children = listToTree(item.Id, arr) } // 取出最顶层的 放到一个新的数组中 tree.push(item) } }) return tree } export function listToTreeLower(parentId, all) { let tree = [] let arr = all arr.forEach(item => { if (item.parentId == parentId) { if (listToTree(item.id, arr).length > 0) { // 递归如果存在children设置children item.children = listToTree(item.id, arr) } // 取出最顶层的 放到一个新的数组中 tree.push(item) } }) return tree }