obj-keypath
v1.1.0
Published
get/set object values using keypaths
Downloads
79
Readme
obj-keypath
get/set object values using keypaths
About
Using dot notation (or the split character of your choice), obj-keypath simplifies getting and setting nested values in objects and arrays.
Install
npm install --save obj-keypath
Usage
var keypath = require('obj-keypath')
var data = {
nested: {
obj: {
arr: [{ cool: 'pizza' }]
}
}
}
var value = keypath.get(data, 'nested.obj.arr.0.cool')
console.log(value) // 'pizza'
keypath.set(data, 'nested.obj.arr.0.cool', 'awesome')
value = keypath.get(data, 'nested.obj.arr.0.cool')
console.log(value) // 'awesome'
You can optionally add a last argument to the get
and set
methods to use a character other than .
to split the keys.
Here's an example using '/', which is useful for working with objects as they relate to file & url paths:
var data = {
nested: {
obj: {
arr: [{ cool: 'pizza' }]
}
}
}
var value = keypath.get(data, 'nested/obj/arr/0/cool', '/')
keypath.set(data, 'nested/obj/arr/0/cool', 'awesome', '/')
Contributing
Contributions are welcome! Please read the contributing guidelines first.
Conduct
It is important that this project contributes to a friendly, safe, and welcoming environment for all. Read this project's code of conduct
Contact
- issues – Please open issues in the issues queue
- email – Need in-depth support via paid contract? Send an email to [email protected]