object-path

Access deep properties using a path

npm install object-path
12 downloads in the last day
82 downloads in the last week
184 downloads in the last month

object-path

Access deep properties using a path

NPM

Build Status Coverage Status devDependency Status

browser support

Install

Node.js

npm install object-path

Browser

bower install object-path

Usage


var obj = {
  a: {
    b: "d",
    c: ["e", "f"]
  }
};

var objectPath = require("object-path");

//get deep property
objectPath.get(obj, "a.b");  //returns "d"

//works also with arrays
objectPath.get(obj, "a.c.1");  //returns "f"

//set
objectPath.set(obj, "a.h", "m");
objectPath.get(obj, "a.h");  //returns "m"

//set will create intermediate object/arrays
objectPath.set(obj, "a.j.0.f", "m");

//push into arrays (and create intermediate objects/arrays)
objectPath.push(obj, "a.k", "o");

//Ensure a path exists (if it doesn't, set the default value you provide)
objectPath.ensureExists(obj, "a.k.1", "DEFAULT");
npm loves you