object-path
Access deep properties using a path
Changelog
0.11.0
- Introduce ability to specify options and create new instances of
object-path
- Introduce option to control the way
object-path
deals with inherited properties (includeInheritedProps
) - New default
object-path
instance already configured to handle not-own object properties (withInheritedProps
)
0.10.0
- Improved performance of
get
,set
, andpush
by 2x-3x - Introduced a benchmarking test suite
- BREAKING CHANGE:
del
,empty
,set
will not affect not-own object's properties (made them consistent with the other methods)
Install
Node.js
npm install object-path --save
Bower
bower install object-path --save
Typescript typings
typings install --save dt~object-path
Usage
var obj = a: b: "d" c: "e" "f" '\u1200': 'unicode key' 'dot.dot': 'key' ; var objectPath = ; //get deep propertyobjectPath; //returns "d"objectPath; //returns "key"objectPath; //returns "unicode key" //get the first non-undefined valueobjectPath; //empty a given path (but do not delete it) depending on their type,so it retains reference to objects and arrays.//functions that are not inherited from prototype are set to null.//object instances are considered objects and just own property names are deletedobjectPath; // obj.a.b is now ''objectPath; // obj.a.c is now []objectPath; // obj.a is now {} //works also with arraysobjectPath; //returns "f"objectPath; //returns "f" //can return a default value with getobjectPath; //returns "DEFAULT", since a.c.b path doesn't exists, if omitted, returns undefined //setobjectPath; // or objectPath.set(obj, ["a","h"], "m");objectPath; //returns "m" //set will create intermediate object/arraysobjectPath; //will insert values in arrayobjectPath; // obj.a.c = ["e", "m", "f"] //push into arrays (and create intermediate objects/arrays)objectPath; //ensure a path exists (if it doesn't, set the default value you provide)objectPath;var oldVal = objectPath; // oldval === "d" //deletes a pathobjectPath; // obj.a.b is now undefinedobjectPath; // obj.a.c is now ['f'] //tests path existenceobjectPath; // trueobjectPath; // false //bind objectvar model = ; //now any method from above is supported directly w/o passing an objectmodel; //returns "d"model; //returns "DEFAULT"model; // obj.a.b is now undefinedmodel; // false
object-path
deals with inherited properties
How By default object-path
will only access an object's own properties. Look at the following example:
var proto = notOwn: prop: 'a'var obj = Object; //This will return undefined (or the default value you specified), because notOwn is//an inherited propertyobjectPath; //This will set the property on the obj instance and not the prototype.//In other words proto.notOwn.prop === 'a' and obj.notOwn.prop === 'b'objectPath;
To configure object-path
to also deal with inherited properties, you need to create a new instance and specify
the includeInheritedProps = true
in the options object:
var objectPath = ;var objectPathWithInheritedProps = objectPath
Alternatively, object-path
exposes an instance already configured to handle inherited properties (objectPath.withInheritedProps
):
var objectPath = ;var objectPathWithInheritedProps = objectPathwithInheritedProps
Once you have the new instance, you can access inherited properties as you access other properties:
var proto = notOwn: prop: 'a'var obj = Object; //This will return 'a'objectPathwithInheritedProps; //This will set proto.notOwn.prop to 'b'objectPath;
Immutability
If you are looking for an immutable alternative of this library, you can take a look at: object-path-immutable
Credits
- Mario Casciaro - Author
- Paulo Cesar - Major contributor