Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

iterate-tree

AngelAngelov79MIT2.0.1

iterate tree structure using Depth-first search(DFS) and Breadth-first search(BFS) algorithms for traversing or searching

iterate, tree, depth-first search, breadth-first search, search

readme

iterate-tree

npm

Iterate tree object by given propety name and callback.

Methods implement Depth-first search(DFS) and Breadth-first search(BFS) algorithms for traversing or searching.

Install

npm install iterate-tree

Traversing

var iterator = require('iterate-tree')

//Breadth-first search(BFS)
iterator.bfs(treeObject, 'propName', (obj) => {

    // Do something with the object

});

//Depth-first search(DFS)
iterator.dfs(treeObject, 'propName', (obj) => {

    // Do something with the object

});

Searching

If method is used as search of an object in the tree structure - RETURN FALSE in the callback breaks the search when it is needed.

var iterator = require('iterate-tree')

//Breadth-first search(BFS)
iterator.bfs(treeObject, 'propName', (obj) => {
    if(obj == condition){

        // Do something with the found object

        return false; // finish the search
    }
});

//Depth-first search(DFS)
iterator.dfs(treeObject, 'propName', (obj) => {
    if(obj == condition){

        // Do something with the found object

        return false; // finish the search
    }
});