Skip to content
A small object graph traversal library
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
dist
test
.gitignore
.travis.yml
README.md
bower.json
index.js
package.json

README.md

#object-traverse

Build Status view on npm

A very simple utility on traversing object graphs using a string.

Changelog

v1.1.1

  • added create and push

v1.1.0

  • used "." instead of slash

v1.0.2

  • added has which check if a path is available by checking if its undefined

How to use

   npm install --save-dev object-traverse
   

If using via normal javascript include, then it is exported as window.objectTraverse.

API

var traverse = require('object-traverse');

var graph = {  
   field : {
      deep : {
         x : 1,
         y: 2
      }
   },
   field2: [],
   deepFunc : { someFunc: function(arg) {} }
   
};
//get a value
traverse(graph).get('field.deep');
//get with a default
traverse(graph).get('field.deep','default return val if not found'); 

//check if something is defined
traverse(graph).has('field.deep.missing');

//sets a value
traverse(graph).set('field.deep.z',5);
   
//helpers
traverse(graph).isArray('field2');
traverse(graph).isString('field2');
traverse(graph).isObject('field2');
traverse(graph).isNumber('field2');
          
//exec
traverse(graph).exec('deepFunc.someFunc','arg');         

//delete
traverse(graph).delete('field.deep.x');

//push - add vall to the array, will create if it doesn't exists
traverse(graph).push('a.b.c',val);

//create create the given path if it doesn't exists, will skip none object types
traverse(graph).create('a.b.c');
traverse(graph).get('a.b.c'); //{ a : { b : { c: {} } }
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.