obj-keypath

1.1.0 • Public • Published

obj-keypath

get/set object values using keypaths

npm travis standard conduct

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

License

ISC

Readme

Keywords

none

Package Sidebar

Install

npm i obj-keypath

Weekly Downloads

2

Version

1.1.0

License

MIT

Last publish

Collaborators

  • sethvincent