remove
Remove a node from a tree by id and return the removed node, detaching its subtree from the parent in a single call.
Usage
The remove method removes a node and all its descendants from the tree by node id. This method mutates the tree array in place.
example.ts
import { Tree } from '@movk/core'
const tree = [{ id: 1, name: 'A', children: [{ id: 2, name: 'B' }] }]
Tree.remove(tree, 2)
/*
tree will be:
[{ id: 1, name: 'A', children: [] }]
*/
API
remove<T extends TreeNode>(tree: T[], id: number | string, config?: TreeConfig): boolean
Parameters
tree
T[] required
The source tree array.
id
number | string required
The ID of the node to remove.
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
boolean
Returns
true if the node was found and removed, otherwise false.Changelog
No recent changes