obj-traverse
Small and lightweight library that enables you to traverse deep nested objects and edit them
Getting Started
- Run ``
npm i obj-traverse --save
``
- Import library:
var objTraverse = require('obj-traverse/lib/obj-traverse');
`` (look at 3a)b) ES6: ``
import * as objTraverse from 'obj-traverse/lib/obj-traverse';
`` (look at 3a)c) ES6: ``
import { findAll } from 'obj-traverse/lib/obj-traverse';
`` (look at 3b)- Use it:
objTraverse.findAll(...)
``b) ``
findAll(...)
``Demo
You can visit http://dominik791.github.io/obj-traverse-demo and play with the libraryAPI
- ``
findFirst(tree, childrenKey, objToFindBy)
``
It iterates through each deep nested object and if finds object that has prop and value specified in ```objToFindBy```
argument, it stops the walk and returns reference to this object. If none is found, it returns false.
<br/>

<br/>
Please note that the method checks all children on current level and then it deepens to the next level - grandchildren.
- ``
findAll(tree, childrenKey, objToFindBy)
``
It iterates through each deep nested object and for every found object that has prop and value specified in
```objToFindBy``` argument, it pushes reference of this object to the result array. When it finishes the walk, it returns the array.
If none is found, it returns false.
<br/>

<br/>
- ``
findAndModifyFirst(tree, childrenKey, objToFindBy, replacementObj)
``
It iterates through each deep nested object and if finds object that has prop and value specified in ```objToFindBy```
argument, it replaces the current object with ```replacementObj```, stops recursive walk and returns reference to the root object.
If none is found, it returns false.
<br/>

<br/>
Please note that the method checks all children on current level and then it deepens to the next level - grandchildren.
- ``
findAndModifyAll(tree, childrenKey, objToFindBy, replacementObj)
``
It iterates through each deep nested object and for every found object that has prop and value specified in
```objToFindBy``` argument, it replaces the current object with ```replacementObj``` and returns reference to the root object.
If none is found, it returns false.
<br/>

<br/>
- ``
findAndDeleteFirst(tree, childrenKey, objToFindBy)
``
It iterates through each deep nested object and if finds object that has prop and value specified in ```objToFindBy```
argument, it deletes it, stops the walk and returns reference to the root object.
If none is found, it returns false.
<br/>

<br/>
Please note that the method checks all children on current level and then it deepens to the next level - grandchildren.
- ``
findAndDeleteAll(tree, childrenKey, objToFindBy)
``
It iterates through each deep nested object and if finds object that has prop and value specified in ```objToFindBy```
argument, it deletes it, continue the walk and returns reference to the root object once finished.
If none of objects is found, it returns false.
<br/>

<br/>
Licence
MITAuthor
- Dominik Broj