2.0.1 • Published 6 years ago

iterate-tree v2.0.1

Weekly downloads
1
License
MIT
Repository
github
Last release
6 years ago

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
    }
});