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 we tested does this, or does it correctly).
Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your ❤️ and support.
Details
Install with npm:
$ npm install --save get-valueSee the unit tests for many more examples.
const get = require('get-value');
const obj = { a: { b: { c: { d: 'foo' } } } };
console.log(get(obj)); //=> { a: { b: { c: { d: 'foo' } } } };
console.log(get(obj, 'a')); //=> { b: { c: { d: 'foo' } } }
console.log(get(obj, 'a.b')); //=> { c: { d: 'foo' } }
console.log(get(obj, 'a.b.c')); //=> { d: 'foo' }
console.log(get(obj, 'a.b.c.d')); //=> 'foo'Unlike other dot-prop libraries, get-value works when keys have dots in them:
console.log(get({ 'a.b': { c: 'd' } }, 'a.b.c'));
//=> 'd'
console.log(get({ 'a.b': { c: { 'd.e': 'f' } } }, 'a.b.c.d.e'));
//=> 'f'console.log(get({ a: { b: { c: { d: 'foo' } } }, e: [{ f: 'g' }, { f: 'h' }] }, 'e.1.f'));
//=> 'h'
console.log(get({ a: { b: [{ c: 'd' }] } }, 'a.b.0.c'));
//=> 'd'
console.log(get({ a: { b: [{ c: 'd' }, { e: 'f' }] } }, 'a.b.1.e'));
//=> 'f'function foo() {}
foo.bar = { baz: 'qux' };
console.log(get(foo));
//=> { [Function: foo] bar: { baz: 'qux' } }
console.log(get(foo, 'bar'));
//=> { baz: 'qux' }
console.log(get(foo, 'bar.baz'));
//=> quxSlighly 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.log(get({ a: { b: 'c' } }, ['a', 'b']));
//=> 'c'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.log(get(obj, 'foo.a.b.c.d', { default: true })); //=> 'e'
console.log(get(obj, 'foo.bar.baz', { default: true })); //=> true
console.log(get(obj, 'foo.bar.baz', { default: false })); //=> false
console.log(get(obj, 'foo.bar.baz', { default: null })); //=> null
// you can also pass the default value as the last argument
// (this is necessary if the default value is an object)
console.log(get(obj, 'foo.a.b.c.d', true)); //=> 'e'
console.log(get(obj, 'foo.bar.baz', true)); //=> true
console.log(get(obj, 'foo.bar.baz', false)); //=> false
console.log(get(obj, 'foo.bar.baz', null)); //=> nullType: 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 = Object.prototype.propertyIsEnumerable;
const options = {
isValid: (key, obj) => isEnumerable.call(obj, key)
};
const obj = {};
Object.defineProperty(obj, 'foo', { value: 'bar', enumerable: false });
console.log(get(obj, 'foo', options)); //=> undefined
console.log(get({}, 'hasOwnProperty', options)); //=> undefined
console.log(get({}, 'constructor', options)); //=> undefined
// without "isValid" check
console.log(get(obj, 'foo', options)); //=> bar
console.log(get({}, 'hasOwnProperty', options)); //=> [Function: hasOwnProperty]
console.log(get({}, 'constructor', options)); //=> [Function: Object]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 path
const options = { split: path => path.split('/') };
console.log(get(obj, 'a.b/c/d', options)); //=> 'e'
// example of using a regex to split the object path
// (removing escaped dots is unnecessary, this is just an example)
const options = { split: path => path.split(/\\?\./) };
console.log(get(obj, 'a\\.b.c.d', options)); //=> 'e'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.log(get(obj, 'a.b/c/d', { separator: '/' }));
//=> 'e'
console.log(get(obj, 'a\\.b.c.d', { separator: /\\?\./ }));
//=> 'e'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 "."
join: segs => segs.join(segs[0] === 'a' ? '/' : '.')
};
console.log(get(obj, 'a.b.c.d', options));
//=> 'e'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.log(get(target, 'a.b.c.d', options));
//=> 'e'(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 (338 bytes)
dot-prop x 2,524,501 ops/sec ±3.47% (90 runs sampled)
dotty x 1,990,042 ops/sec ±1.10% (91 runs sampled)
get-value x 3,776,247 ops/sec ±0.71% (98 runs sampled)
getobject x 1,166,194 ops/sec ±2.94% (94 runs sampled)
object-path x 975,380 ops/sec ±0.27% (97 runs sampled)
fastest is get-value (by 50% avg)
# root (215 bytes)
dot-prop x 18,774,512 ops/sec ±0.67% (95 runs sampled)
dotty x 16,732,378 ops/sec ±0.66% (95 runs sampled)
get-value x 35,516,146 ops/sec ±1.16% (92 runs sampled)
getobject x 7,743,671 ops/sec ±2.99% (95 runs sampled)
object-path x 11,955,285 ops/sec ±0.48% (95 runs sampled)
fastest is get-value (by 89% avg)
# shallow (91 bytes)
dot-prop x 10,195,874 ops/sec ±0.88% (95 runs sampled)
dotty x 8,383,019 ops/sec ±0.81% (97 runs sampled)
get-value x 9,891,229 ops/sec ±0.88% (90 runs sampled)
getobject x 4,333,202 ops/sec ±1.52% (99 runs sampled)
object-path x 4,568,894 ops/sec ±1.60% (94 runs sampled)
fastest is dot-prop (by 3% avg)
Clone this library into a local directory:
$ git clone https://github.com/jonschlinkert/get-value.gitThen install devDependencies and run benchmarks:
$ npm install && node benchmark- Refactored to typescript
- Added support for handling deep property paths with arrays
- Improved performance on large nested objects
- Fixed edge case issues with keys containing special characters.
- Updated benchmarks
- Updated documentation to reflect new features and bug fixes.
- Improved support for escaping. It's no longer necessary to use backslashes to escape keys.
- Adds
options.defaultfor defining a default value to return when no value is resolved. - Adds
options.isValidto allow the user to check the object after each iteration. - Adds
options.separatorfor customizing character to split on. - Adds
options.splitfor customizing how the object path is split. - Adds
options.joinfor customizing how the object path is joined when iterating over path segments. - Adds
options.joinCharfor customizing the join character.
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 testBuilding 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 && verbYou might also be interested in these projects:
- has-any-deep: Return true if
keyexists 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: Set nested properties on an object using dot notation. | homepage
- unset-value: Delete nested properties from an object using dot notation. | homepage
| Commits | Contributor |
|---|---|
| 93 | jonschlinkert |
| 2 | doowb |
| 2 | felladrin |
| 1 | onokumus |
| 1 | joepie91 |
| 1 | sonofmagic |
Jon Schlinkert
Copyright © 2025, Jon Schlinkert. Released under the MIT License.
This file was generated by verb-generate-readme, v0.8.0, on February 05, 2025.