Tree
/ API Reference
Tree
/ API Reference

insert

Inserts a node at a specific index in a parent node.

The Gist

import { create, insert } from '@weser/tree'

const newNode = create({
  children: [],
})

const newTree = insert(tree, 'parent-node-id', 0, newNode)

Parameters

ParameterTypeDescription
rootNodeTThe root node of the tree.
idstringThe id of the parent node to which the new node will be inserted.
indexnumberThe index at which the new node will be inserted.
newNodeTThe node to insert.

Returns

(T) The updated root node with the new node inserted at the specified index in the parent node.

On this page