Skip to content

chennlang/walk-tree-list

Repository files navigation

walk-tree-list

Traverse tree list

Install

# npm
npm install walk-tree-list

# yarn
yarn add walk-tree-list

# pnpm
pnpm install walk-tree-list

Usage

Base

import walkTree from "walk-tree-list";

const treeList = [{ name: "dir1", children: [{ name: "dir2" }] }];

walkTree(treeList, (node, pNode) => {
  console.log(node, pNode);
});

KeyMapping

import walkTree from "walk-tree-list";

const treeList = [{ name: "dir1", childList: [{ name: "dir2" }] }];

walkTree(
  treeList,
  (node, pNode) => {
    console.log(node, pNode);
  },
  { children: "childList" }
);

Other

import walkTree from "walk-tree-list";

const treeList = [{ name: "dir1", children: [{ name: "dir2" }] }];

walkTree(treeList, (node, pNode) => {
  // level === 1
  if (!pNode) {
    console.log("this is first level node");
  }

  // Skip the subsequent traversal
  if (node.name === "dir1") return "continue";

  // Node found, ending early.
  if (node.name === "dir1") return "end";
});