obj-traverse

Small and lightweight library that enables you to traverse deep nested objects and edit them

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
obj-traverse
10061.0.06 years ago6 years agoMinified + gzip package size for obj-traverse in KB

Readme

obj-traverse
Small and lightweight library that enables you to traverse deep nested objects and edit them

Getting Started

  1. Run ``npm i obj-traverse --save``
  1. Import library:
a) ``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)
  1. Use it:
a) `` objTraverse.findAll(...) ``
b) `` findAll(...) ``

Demo

You can visit http://dominik791.github.io/obj-traverse-demo and play with the library

API

  1. ``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/>
![findFirst](./demo_gifs/1.findFirst.gif)
<br/>
Please note that the method checks all children on current level and then it deepens to the next level - grandchildren.
  1. ``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/>
![findAll](./demo_gifs/2.findAll.gif)
<br/>
  1. ``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/>
![findAndModifyFirst](./demo_gifs/3.findAndModifyFirst.gif)
<br/>
Please note that the method checks all children on current level and then it deepens to the next level - grandchildren.
  1. ``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/>
![findAndModifyAll](./demo_gifs/4.findAndModifyAll.gif)
<br/>
  1. ``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/>
![findAndDeleteFirst](./demo_gifs/5.findAndDeleteFirst.gif)
<br/>
Please note that the method checks all children on current level and then it deepens to the next level - grandchildren.
  1. ``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/>
![findAndDeleteAll](./demo_gifs/6.findAndDeleteAll.gif)
<br/>

Licence

MIT

Author

  • Dominik Broj