瀏覽代碼

feat(tree): 增加方法findByField

vian 4 年之前
父節點
當前提交
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();
     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节点
   // 查找ID节点
   find(ID: string): TreeNode<T> | null {
   find(ID: string): TreeNode<T> | null {
     return this.IDMap[ID] || null;
     return this.IDMap[ID] || null;