get-value
Use property paths like 'a.b.c' to get a nested value from an object. Even works when keys have dots in them (no other dot-prop library can do this!).
Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your ❤️ and support.
Table of Contents
Details
Install
Install with npm:
$ npm install --save get-value
Usage
See the unit tests for many more examples.
const get = ;const obj = a: b: c: d: 'foo' ; console; //=> { a: { b: { c: { d: 'foo' } } } };console; //=> { b: { c: { d: 'foo' } } }console; //=> { c: { d: 'foo' } }console; //=> { d: 'foo' }console; //=> 'foo'
Supports keys with dots
Unlike other dot-prop libraries, get-value works when keys have dots in them:
console;//=> 'd' console;//=> 'f'
Supports arrays
console; //=> 'h' console; //=> 'f' console;//=> 'f'
Supports functions
{}foobar = baz: 'qux' ; console; //=> { [Function: foo] bar: { baz: 'qux' } } console; //=> { baz: 'qux' } console; //=> qux
Supports passing object path as an array
Slighly improve performance by passing an array of strings to use as object path segments (this is also useful when you need to dynamically build up the path segments):
console;//=> 'c'
Options
options.default
Type: any
Default: undefined
The default value to return when get-value cannot resolve a value from the given object.
const obj = foo: a: b: c: d: 'e' ;console; //=> 'e'console; //=> trueconsole; //=> falseconsole; //=> null // you can also pass the default value as the last argument// (this is necessary if the default value is an object)console; //=> 'e'console; //=> trueconsole; //=> falseconsole; //=> null
options.isValid
Type: function
Default: true
If defined, this function is called on each resolved value. Useful if you want to do .hasOwnProperty
or Object.prototype.propertyIsEnumerable
.
const isEnumerable = ObjectprototypepropertyIsEnumerable;const options = isEnumerable; const obj = {};Object; console; //=> undefinedconsole; //=> undefinedconsole; //=> undefined // without "isValid" checkconsole; //=> barconsole; //=> [Function: hasOwnProperty]console; //=> [Function: Object]
options.split
Type: function
Default: String.split()
Custom function to use for splitting the string into object path segments.
const obj = 'a.b': c: d: 'e' ; // example of using a string to split the object pathconst options = path ;console; //=> 'e' // example of using a regex to split the object path// (removing escaped dots is unnecessary, this is just an example)const options = path ;console; //=> 'e'
options.separator
Type: string|regex
Default: .
The separator to use for spliting the string (this is probably not needed when options.split
is used).
const obj = 'a.b': c: d: 'e' ; console; //=> 'e' console; //=> 'e'
options.join
Type: function
Default: Array.join()
Customize how the object path is created when iterating over path segments.
const obj = 'a/b': c: d: 'e' ;const options = // when segs === ['a', 'b'] use a "/" to join, otherwise use a "." segs; console;//=> 'e'
options.joinChar
Type: string
Default: .
The character to use when re-joining the string to check for keys with dots in them (this is probably not needed when options.join
is used). This can be a different value than the separator, since the separator can be a string or regex.
const target = 'a-b': c: d: 'e' ;const options = joinChar: '-' ;console; //=> 'e'
Benchmarks
(benchmarks were run on a MacBook Pro 2.5 GHz Intel Core i7, 16 GB 1600 MHz DDR3).
get-value is more reliable and has more features than dot-prop, without sacrificing performance.
# deep (175 bytes)
dot-prop x 883,166 ops/sec ±0.93% (86 runs sampled)
get-value x 1,448,928 ops/sec ±1.53% (87 runs sampled)
getobject x 213,797 ops/sec ±0.85% (90 runs sampled)
object-path x 184,347 ops/sec ±2.48% (85 runs sampled)
fastest is get-value (by 339% avg)
# root (210 bytes)
dot-prop x 3,905,828 ops/sec ±1.36% (87 runs sampled)
get-value x 16,391,934 ops/sec ±1.43% (83 runs sampled)
getobject x 1,200,021 ops/sec ±1.81% (88 runs sampled)
object-path x 2,788,494 ops/sec ±1.81% (86 runs sampled)
fastest is get-value (by 623% avg)
# shallow (84 bytes)
dot-prop x 2,553,558 ops/sec ±0.89% (89 runs sampled)
get-value x 3,070,159 ops/sec ±0.88% (90 runs sampled)
getobject x 726,670 ops/sec ±0.81% (86 runs sampled)
object-path x 922,351 ops/sec ±2.05% (86 runs sampled)
fastest is get-value (by 219% avg)
Running the benchmarks
Clone this library into a local directory:
$ git clone https://github.com/jonschlinkert/get-value.git
Then install devDependencies and run benchmarks:
$ npm install && node benchmark
Release history
v3.0.0
- Improved support for escaping. It's no longer necessary to use backslashes to escape keys.
- Adds
options.default
for defining a default value to return when no value is resolved. - Adds
options.isValid
to allow the user to check the object after each iteration. - Adds
options.separator
for customizing character to split on. - Adds
options.split
for customizing how the object path is split. - Adds
options.join
for customizing how the object path is joined when iterating over path segments. - Adds
options.joinChar
for customizing the join character.
About
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
Running Tests
Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:
$ npm install && npm test
Building docs
(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)
To generate the readme, run the following command:
$ npm install -g verbose/verb#dev verb-generate-readme && verb
Related projects
You might also be interested in these projects:
- has-any-deep: Return true if
key
exists deeply on the given object. | homepage - has-any: Returns true if an object has any of the specified keys. | homepage
- has-value: Returns true if a value exists, false if empty. Works with deeply nested values using… more | homepage
- set-value: Create nested values and any intermediaries using dot notation (
'a.b.c'
) paths. | homepage - unset-value: Delete nested properties from an object using dot notation. | homepage
Contributors
Commits | Contributor |
---|---|
81 | jonschlinkert |
2 | ianwalter |
1 | doowb |
Author
Jon Schlinkert
License
Copyright © 2018, Jon Schlinkert. Released under the MIT License.
This file was generated by verb-generate-readme, v0.6.0, on March 07, 2018.