Access deep properties using a path
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");