Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

object-traverse

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

object-traverse

Parse , check and traverse javascript objects via string path

  • 1.1.1
  • latest
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

#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: {} } }

Keywords

FAQs

Package last updated on 28 Apr 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc