Small and lightweight library that enables you to traverse deep nested objects and edit them
- Run
npm i obj-traverse --save
- 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) - Use it:
a)objTraverse.findAll(...)
b)findAll(...)
You can visit http://dominik791.github.io/obj-traverse-demo and play with the library
-
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.
![findFirst](./demo_gifs/1.findFirst.gif)
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.
![findAll](./demo_gifs/2.findAll.gif)
-
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 withreplacementObj
, stops recursive walk and returns reference to the root object. If none is found, it returns false.
![findAndModifyFirst](./demo_gifs/3.findAndModifyFirst.gif)
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 withreplacementObj
and returns reference to the root object. If none is found, it returns false.
![findAndModifyAll](./demo_gifs/4.findAndModifyAll.gif)
-
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.
![findAndDeleteFirst](./demo_gifs/5.findAndDeleteFirst.gif)
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.
![findAndDeleteAll](./demo_gifs/6.findAndDeleteAll.gif)
MIT
- Dominik Broj