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 :heart: and support.
Slightly improves 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):
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 = { split: path => path.split('/') };console.log(getValue(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(getValue(obj, 'a\\.b.c.d', options)); //=> 'e'
options.separator
Type: string|regex
Default: .
The separator to use for splitting the string (this is probably not needed when options.split is used).
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(getValue(obj, 'a.b.c.d', options));//=> '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.
Adds support for Map-like objects, allowing objects with a get method that can be used to retrieve values by key.
Removed outdated travis config, and added GitHub Actions workflow for running tests.
v4.0.0
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.
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.
Please read the contributing guide for advice on opening issues, pull requests, and coding standards.
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: