Git Product home page Git Product logo

obj-traverse's Introduction

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

obj-traverse's People

Contributors

brojd avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.