Просмотр исходного кода

feat(tree): 增加方法findByField

vian 4 лет назад
Родитель
Сommit
6b2c6b21c9
1 измененных файлов с 6 добавлено и 0 удалено
  1. 6 0
      tree/src/tree.ts

+ 6 - 0
tree/src/tree.ts

@@ -153,6 +153,12 @@ export class Tree<T extends TreeRaw = TreeRaw> {
     this.reGenData();
   }
 
+  // 根据字段值查找节点
+  // eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
+  findByField(field: string, value: any): TreeNode<T> | null {
+    return this.data.find(item => item[field] === value) || null;
+  }
+
   // 查找ID节点
   find(ID: string): TreeNode<T> | null {
     return this.IDMap[ID] || null;