Module: tree

tree相关函数

Functions

arrayToTree

arrayToTree(array, idKey?, pidKey?, options?): Record<UnknownMapKey, any>

使用高性能算法,将array结构数据变为tree结构数据

Example

//生成测试数据function addChildren(count,parent){ const data = []; const pid = parent?parent.id:null; const parentName = parent?parent.name+'-':''; _.each(_.range(0,count),i=>{   const sortNo = _.randi(0,count);   data.push({id:_.alphaId(),pid,name:parentName+i,sortNo}) }); return data;}function genTree(depth,parents,data){ _.each(parents,r=>{   const children = addChildren(_.randi(1,4),r);   if(depth-1>0){     genTree(depth-1,children,data);   }   _.append(data,...children); });}const roots = addChildren(2);const data = [];genTree(2,roots,data);_.insert(data,0,...roots);const tree = _.arrayToTree(data,'id','pid',{attrMap:{text:'name'}});_.walkTree(tree,(parentNode,node,chain)=>console.log('node',node.text,'sortNo',node.sortNo,'chain',_.map(chain,n=>n.name)));

Since

1.5.0

Parameters

NameTypeDefault valueDescription
arrayRecord<UnknownMapKey, any>undefined原始数据集。如果非Array类型,返回空数组
idKeystring'id'id标识
pidKey?stringundefined-
optionsObject{}自定义选项
options.attrMap?Record<string, any>undefined转换tree节点时的属性映射,如{text:'name'}表示把array中一条记录的name属性映射为tree节点的text属性
options.childrenKey?stringundefined包含子节点容器的key。默认'children'
options.rootParentValue?anyundefined根节点的parentValue,用于识别根节点。默认null
options.sortKey?stringundefined如果指定排序字段,则会在转换tree时自动排序。字段值可以是数字或字符等可直接进行比较的类型。性能高于转换后再排序

Returns

Record<UnknownMapKey, any>

返回转换好的顶级节点数组或空数组


closest

closest(node, predicate, parentKey): Record<UnknownMapKey, any> | null

根据指定的node及parentKey属性,查找最近的祖先节点

Since

2.2.0

Parameters

NameTypeDescription
nodeRecord<UnknownMapKey, any>Element节点或普通对象节点
predicate(node: Record<UnknownMapKey, any>, times: number, cancel: () => void) => boolean(node,times,cancel)断言函数,如果返回true表示节点匹配。或调用cancel中断查找
parentKeystring父节点引用属性名

Returns

Record<UnknownMapKey, any> | null

断言为true的最近一个祖先节点


filterTree

filterTree(treeNodes, predicate, options?): Record<UnknownMapKey, any>

类似findTreeNodes,但会返回包含所有父节点的节点副本数组,已做去重处理。 结果集可用于重新构建tree

Example

//生成测试数据function addChildren(count,parent){ const data = []; const pid = parent?parent.id:null; const parentName = parent?parent.name+'-':''; _.each(_.range(0,count),i=>{   const sortNo = _.randi(1,4);   data.push({id:_.alphaId(),pid,name:parentName+i,sortNo}) }); return data;}function genTree(depth,parents,data){ _.each(parents,r=>{   const children = addChildren(_.randi(1,4),r);   if(depth-1>0){     genTree(depth-1,children,data);   }   _.append(data,...children); });}const roots = addChildren(2);const data = [];genTree(2,roots,data);_.insert(data,0,...roots);const tree = _.arrayToTree(data,'id','pid',{sortKey:'sortNo'});_.each(_.filterTree(tree,node=>node.sortNo>1),node=>console.log(_.omit(node,'children','id','pid')))

Since

1.5.0

Parameters

NameTypeDescription
treeNodesRecord<UnknownMapKey, any> | Record<UnknownMapKey, any>一组节点或一个节点
predicate(node: Record<UnknownMapKey, any>) => boolean | NonFuncItee(node) 断言
当断言是函数时回调参数见定义
其他类型请参考 iteratee
options?Object自定义选项
options.childrenKey?string包含子节点容器的key。默认'children'

Returns

Record<UnknownMapKey, any>

找到的符合条件的所有节点副本或空数组


findTreeNode

findTreeNode(treeNodes, predicate, options?): Record<UnknownMapKey, any> | undefined

查找给定节点及所有子孙节点中符合断言的第一个节点并返回

Example

//生成测试数据function addChildren(count,parent){ const data = []; const pid = parent?parent.id:null; const parentName = parent?parent.name+'-':''; _.each(_.range(0,count),i=>{   const sortNo = _.randi(0,count);   data.push({id:_.alphaId(),pid,name:parentName+i,sortNo}) }); return data;}function genTree(depth,parents,data){ _.each(parents,r=>{   const children = addChildren(_.randi(2,5),r);   if(depth-1>0){     genTree(depth-1,children,data);   }   _.append(data,...children); });}const roots = addChildren(2);const data = [];genTree(4,roots,data);_.insert(data,0,...roots);const tree = _.arrayToTree(data,'id','pid',{sortKey:'sortNo'});console.log(_.omit(_.findTreeNode(tree,node=>node.sortNo>2),'children','id','pid'))

Since

1.5.0

Parameters

NameTypeDescription
treeNodesRecord<UnknownMapKey, any> | Record<UnknownMapKey, any>一组节点或一个节点
predicate(node: Record<UnknownMapKey, any>) => boolean | NonFuncItee(node) 断言
当断言是函数时回调参数见定义
其他类型请参考 iteratee
options?Object自定义选项
options.childrenKey?string包含子节点容器的key。默认'children'

Returns

Record<UnknownMapKey, any> | undefined

第一个匹配断言的节点或undefined


findTreeNodes

findTreeNodes(treeNodes, predicate, options?): Record<UnknownMapKey, any>

查找给定节点及所有子孙节点中符合断言的所有节点并返回

Example

//生成测试数据function addChildren(count,parent){ const data = []; const pid = parent?parent.id:null; const parentName = parent?parent.name+'-':''; _.each(_.range(0,count),i=>{   const sortNo = _.randi(0,count);   data.push({id:_.alphaId(),pid,name:parentName+i,sortNo}) }); return data;}function genTree(depth,parents,data){ _.each(parents,r=>{   const children = addChildren(_.randi(2,5),r);   if(depth-1>0){     genTree(depth-1,children,data);   }   _.append(data,...children); });}const roots = addChildren(2);const data = [];genTree(3,roots,data);_.insert(data,0,...roots);const tree = _.arrayToTree(data,'id','pid',{sortKey:'sortNo'});_.each(_.findTreeNodes(tree,node=>node.sortNo>2),node=>console.log(_.omit(node,'children','id','pid')))

Since

1.5.0

Parameters

NameTypeDescription
treeNodesRecord<UnknownMapKey, any> | Record<UnknownMapKey, any>一组节点或一个节点
predicate(node: Record<UnknownMapKey, any>) => boolean | NonFuncItee(node) 断言
当断言是函数时回调参数见定义
其他类型请参考 iteratee
options?Object自定义选项
options.childrenKey?string包含子节点容器的key。默认'children'

Returns

Record<UnknownMapKey, any>

找到的符合条件的所有节点或空数组


sortTree

sortTree(treeNodes, comparator, options?): void

对给定节点及所有子孙节点(同级)排序

Example

//生成测试数据function addChildren(count,parent){ const data = []; const pid = parent?parent.id:null; const parentName = parent?parent.name+'-':''; _.each(_.range(0,count),i=>{   const sortNo = _.randi(0,9);   data.push({id:_.alphaId(),pid,name:parentName+i,sortNo}) }); return data;}function genTree(depth,parents,data){ _.each(parents,r=>{   const children = addChildren(_.randi(1,4),r);   if(depth-1>0){     genTree(depth-1,children,data);   }   _.append(data,...children); });}const roots = addChildren(1);const data = [];genTree(2,roots,data);_.insert(data,0,...roots);let tree = _.arrayToTree(data,'id','pid');console.log('Before sort---------------');_.walkTree(_.cloneDeep(tree),(parentNode,node,chain)=>console.log('node',node.name,'sortNo',node.sortNo))_.sortTree(tree,(a,b)=>a.sortNo - b.sortNo);console.log('After sort---------------');_.walkTree(tree,(parentNode,node,chain)=>console.log('node',node.name,'sortNo',node.sortNo))

Since

1.5.0

Parameters

NameTypeDescription
treeNodesRecord<UnknownMapKey, any> | Record<UnknownMapKey, any>一组节点或一个节点
comparator(a: Record<UnknownMapKey, any>, b: Record<UnknownMapKey, any>) => number(a,b) 排序函数
options?Object自定义选项
options.childrenKey?string包含子节点容器的key。默认'children'

Returns

void


walkTree

walkTree(treeNodes, callback, options?): void

以给定节点为根遍历所有子孙节点。深度优先

Example

//生成测试数据function addChildren(count,parent){ const data = []; const pid = parent?parent.id:null; const parentName = parent?parent.name+'-':''; _.each(_.range(0,count),i=>{   const sortNo = _.randi(0,count);   data.push({id:_.alphaId(),pid,name:parentName+i,sortNo}) }); return data;}function genTree(depth,parents,data){ _.each(parents,r=>{   const children = addChildren(_.randi(1,4),r);   if(depth-1>0){     genTree(depth-1,children,data);   }   _.append(data,...children); });}const roots = addChildren(2);const data = [];genTree(2,roots,data);_.insert(data,0,...roots);const tree = _.arrayToTree(data,'id','pid',{sortKey:'sortNo'});_.walkTree(tree,(parentNode,node,chain)=>console.log('node',node.name,'sortNo',node.sortNo,'chain',_.map(chain,n=>n.name)))

Since

1.5.0

Parameters

NameTypeDescription
treeNodesRecord<UnknownMapKey, any> | Record<UnknownMapKey, any>一组节点或一个节点
callback(parentNode: Record<UnknownMapKey, any>, node: Record<UnknownMapKey, any>, chain: Record<UnknownMapKey, any>) => number | boolean | void(parentNode,node,chain)回调函数,如果返回false则中断遍历,如果返回-1则停止分支遍历
options?Record<UnknownMapKey, any>自定义选项

Returns

void