• Stars
    star
    103
  • Rank 333,046 (Top 7 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created almost 8 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

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
  2. 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)
  3. Use it:
    a) objTraverse.findAll(...)
    b) findAll(...)

Demo

You can visit https://brojd.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.

    findFirst

    Please note that the method checks all children on current level and then it deepens to the next level - grandchildren.

  2. 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

  3. 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.

    findAndModifyFirst

    Please note that the method checks all children on current level and then it deepens to the next level - grandchildren.

  4. 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.

    findAndModifyAll

  5. 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

    Please note that the method checks all children on current level and then it deepens to the next level - grandchildren.

  6. 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

Licence

MIT

Author

  • Dominik Broj