Files
buy-lite/web/src/utils/tools.js
2023-01-28 18:08:41 +08:00

32 lines
864 B
JavaScript

import {mainStore} from "./store";
export const getToken = () => mainStore()?.token
export const $confirm = () => {
}
/**
* 数组转tree
* @param list 待转化的数组
* @param config 配置
*/
const $arr2tree = (list, config = {}) => {
const {key = 'id', parent = 'parentId', children = 'children'} = config, result = [], itemMap = {}, ids = list?.map(e => `${e[key]}`)?.toString()
for (const e of list) {
const id = e[key], pid = e[parent]
itemMap[id] = {...e, [children]: [itemMap[id]?.[children]].flat().filter(Boolean)}
const treeItem = itemMap[id]
if (!!pid && ids.indexOf(pid) > -1) {
if (!itemMap[pid]) {
itemMap[pid] = {
children: [],
}
}
itemMap[pid].children.push(treeItem)
} else result.push(treeItem)
}
return result
}
export default {
getToken, $confirm, $arr2tree
}