Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | 6x 6x 6x 11x 6x 11x 11x 5x 5x 4x 4x 4x 7x 6x 5x 7x 7x 1x 5x 10x 1x 5x | /**
* 根据 parentKey 将一维数组构建为树。
* @param {Array} list 原始列表
* @param {string} idKey 节点 ID 字段名,默认 "b_id"
* @param {string} parentKey 父节点关联字段名,默认 "b_parent_id"
* @param {string} childrenKey 子节点字段名,默认 "children"
*/
export function buildTree(
list,
idKey = 'b_id',
parentKey = 'b_parent_id',
childrenKey = 'children',
) {
const map = new Map()
const roots = []
for (const node of list) {
map.set(String(node[idKey]), node)
}
for (const node of list) {
const pid = node[parentKey]
if (pid != null) {
const parent = map.get(String(pid))
if (parent) {
Eif (!parent[childrenKey]) parent[childrenKey] = []
parent[childrenKey].push(node)
continue
}
}
roots.push(node)
}
return roots
}
/**
* 按 b_xh 升序、b_id 升序 递归排序树。
* @param {Array} tree 树形数组
* @param {string} xhKey 排序权重字段名,默认 "b_xh"
* @param {string} idKey 排序兜底字段名,默认 "b_id"
*/
export function sortTree(tree, xhKey = 'b_xh', idKey = 'b_id') {
tree.sort((a, b) => {
const xhDiff = (Number(a[xhKey]) || 0) - (Number(b[xhKey]) || 0)
if (xhDiff !== 0) return xhDiff
return String(a[idKey] || '').localeCompare(String(b[idKey] || ''))
})
for (const node of tree) {
if (node.children?.length) {
sortTree(node.children, xhKey, idKey)
}
}
return tree
}
|