findById
Find a single tree node by its id using the configured id field, returning the node or undefined when no match exists.
Usage
The findById method quickly finds and returns a node by its id. This is an efficient lookup operation.
example.ts
import { Tree } from '@movk/core'
const tree = [{ id: 1, name: 'A', children: [{ id: 2, name: 'B' }] }]
const node = Tree.findById(tree, 2)
// node => { id: 2, name: 'B' }
API
findById<T extends TreeNode>(tree: T[], id: number | string, config?: TreeConfig): T | undefined
Parameters
tree
T[] required
The source tree array.
id
number | string required
The ID of the node to find.
config
TreeConfig
Configuration object for customizing the
id, pid, children key names in the tree structure.id
string
Optional. The key name for the node's unique identifier. Defaults to "id".
pid
string
Optional. The key name for the node's parent identifier. Defaults to "pid".
children
string
Optional. The key name for the children array. Defaults to "children".
Returns
T | undefined
Returns the found node, or
undefined if no match is found.Changelog
No recent changes