dot2val
Set or get a value within a deeply nested object using `dot' notation
Installation
npm install --save dot2val
Usage
var dot2val = require('dot2val');
Given:
var obj = {
prop1: {
arr: ['a', 'b', 'c']
, str: 'Hello'
}
, prop2: {
arr: [ { nested: 'Universe' } ]
, str: 'Hello again!'
}
}
var arr = [ { foo: 'bar' } ];
Expect:
get
dot2val.get(obj, 'prop1.str');
dot2val.get(obj, 'prop1.arr.2');
dot2val.get(obj, 'prop2.arr.0.nested');
dot2val.get(arr, '0.foo');
dot2val.get(undefined, 'doesnt.matter');
dot2val.get({}, 'doesnt.exist');
dot2val.get({}, 'doesnt.exist', 'default value');
set
dot2val.set(obj, 'hello.universe', 'properties');
dot2val.set(obj, 'hello1', 'universe');
dot2val.set(obj, 'prop2');
Try dot2val in your browser
License
MIT