CxJS

findTreePath

import { findTreePath } from 'cx/data'; Copied

Finds a node in a tree structure and returns the path (array of ancestors including the found node) from the root to that node.

Signature

function findTreePath<T>(
  array: T[],
  criteria: (node: T) => boolean,
  childrenField: keyof T
): T[] | false

Parameters

ParameterTypeDescription
arrayT[]Tree data array to search.
criteriafunctionPredicate returning true for the target node.
childrenFieldkeyof TName of the property containing child nodes.

Return Value

Returns an array of nodes from root to the found node (inclusive), or false if no matching node is found.

Examples

Find path to a node by ID

const tree = [
  {
    id: 1,
    name: "Root",
    $children: [
      {
        id: 2,
        name: "Child",
        $children: [
          { id: 3, name: "Grandchild", $children: [] }
        ]
      }
    ]
  }
];

const path = findTreePath(tree, (node) => node.id === 3, "$children");
// [
//   { id: 1, name: "Root", ... },
//   { id: 2, name: "Child", ... },
//   { id: 3, name: "Grandchild", ... }
// ]

Build breadcrumb navigation

const path = findTreePath(categories, (c) => c.id === selectedId, "children");

if (path) {
  const breadcrumb = path.map((node) => node.name).join(" > ");
  // "Electronics > Computers > Laptops"
}

Get parent of a node

const path = findTreePath(tree, (node) => node.id === targetId, "$children");

if (path && path.length > 1) {
  const parent = path[path.length - 2];
  console.log("Parent:", parent.name);
}

Check if node is at root level

const path = findTreePath(tree, (node) => node.id === targetId, "$children");

if (path && path.length === 1) {
  console.log("Node is at root level");
}

See Also