Get or set a value in an object/array using a dot-delimited string or array of keys.
npm install jaunt> Get or set a value in an object/array using a dot-delimited string or array of keys.
Returns the value in obj corresponding to path. Returns undefined if path does not exist.
- obj — An object or array.
- path — A dot-delimited string of keys, or an array of keys.
``js
var obj = {
foo: {
bar: ['Hello', 'World'],
baz: 'Goodbye'
}
};
jaunt.get(obj, 'foo.bar'); //=> ['Hello', 'World']
jaunt.get(obj, 'foo.baz'); //=> 'Goodbye'
jaunt.get(obj, 'foo.bar.0'); //=> 'Hello'
jaunt.get(obj, 'foo.baz.0'); //=> 'Goodbye'
jaunt.get(obj, ['foo', 'bar', 0]); //=> 'World'
jaunt.get(obj, ['foo', 'baz', 0]); //=> 'Goodbye'
jaunt.get(obj, 'invalid'); //=> undefined
`
There can be a trailing “0” in path if it corresponds to a leaf node. So, in the example above, the paths foo.baz and foo.baz.0 are equivalent.
Sets the element corresponding to path in the obj to the specified val. Any “intermediate” elements in the path will be created if they do not exist. Returns the modified obj.
- obj — An object or array.path
- — A dot-delimited string of keys, or an array of keys.val
- — The value to set the element corresponding to path.
`js
var obj = {
foo: {
bar: ['Hello', 'World']
}
};
jaunt.set(obj, 'foo.bar.0', 'Hola');
/* =>
* {
* foo: {
* bar: ['Hola', 'World']
* }
* }
*/
jaunt.set(obj, 'baz', 'Shiny!');
/* =>
* {
* foo: {
* bar: ['Hola', 'World']
* },
* baz: 'Shiny!'
* }
*/
`
Install via npm:
`bash`
$ npm i --save jaunt
Install via bower:
`bash`
$ bower i --save yuanqing/jaunt
To use Jaunt in the browser, include the minified script in your HTML:
`html`
- 1.3.0
- Allow trailing “0” in path for the get` method
- 1.2.0
- Migrate tests to tape
- 1.1.3
- Expose module for use in the browser
- Add minified version of the module
- Add bower.json
- 1.0.0
- Initial release