insertAfter
Insert a new node immediately after a target node in a tree by id, returning whether the insertion succeeded.
Usage
The insertAfter method inserts a new node immediately after the target node (found by id). This method mutates the tree array in place.
example.ts
import { Tree } from '@movk/core'
const tree = [{ id: 1, name: 'A' }, { id: 2, name: 'C' }]
const newNode = { id: 3, name: 'B' }
Tree.insertAfter(tree, newNode, 1)
/*
tree will be:
[{ id: 1, name: 'A' }, { id: 3, name: 'B' }, { id: 2, name: 'C' }]
*/
API
insertAfter<T extends TreeNode>(tree: T[], node: T, targetId: number | string, config?: TreeConfig): boolean
Parameters
tree
T[] required
The source tree array.
node
T required
The new node to insert.
targetId
number | string required
The ID of the target node. The new node will be inserted after this node.
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 target was found and the new node was successfully inserted, otherwise false.Changelog
No recent changes