Deep property access by dot separated string or key array
npm install deep-property-accessbash
npm install --save deep-property-access
`
`javascript
var DeepPropertyAccess = require('deep-property-access');
`Retrieve a value from an arbitrary depth property:
`javascript
var value = DeepPropertyAccess('path.to.my.key');
`or
`javascript
var value = DeepPropertyAccess( ['path', 'to', 'my', 'key' ] );
`If the path cannot be resolved to a valid key the return value is
undefined.
Example
Running the included example file
`javascript
var util = require('util');
var deep = require('../');var testObject = {
key1: {
subkey1: "Some string",
subkey2: true
},
key2: {
subkey3: {
subsubkey1: 42
}
},
key3: [ 24, -10, true ]
}
console.log("Object:\n" + util.inspect(testObject, { depth: 8, colors: true }) + "\n");
var property1 = "key1.subkey2";
var property2 = [ "key2", "subkey3", "subsubkey1" ];
var property3 = "key2";
var property4 = "key3.1";
console.log("Property '" + property1 + "': " + deep(testObject, property1));
console.log("Property '[ '" + property2.join("', '") + "' ]': " + deep(testObject, property2));
console.log("Property '" + property3 + "': " + util.inspect(deep(testObject, property3), { depth: 8, colors: true }));
console.log("Property '" + property4 + "': " + deep(testObject, property4));
`will result in this:
`javascript
Object:
{ key1: { subkey1: 'Some string', subkey2: true },
key2: { subkey3: { subsubkey1: 42 } },
key3: [ 24, -10, true ] }Property 'key1.subkey2': true
Property '[ 'key2', 'subkey3', 'subsubkey1' ]': 42
Property 'key2': { subkey3: { subsubkey1: 42 } }
Property 'key3.1': -10
``Find this project on: GitHub or npm
Copyright 2015 Stefan Hamminga - prjct.net