https://github.com/zoubin/getp
Get nested properties
https://github.com/zoubin/getp
Last synced: 9 months ago
JSON representation
Get nested properties
- Host: GitHub
- URL: https://github.com/zoubin/getp
- Owner: zoubin
- Created: 2015-09-04T02:34:47.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2015-10-07T16:52:40.000Z (over 10 years ago)
- Last Synced: 2025-02-09T05:17:04.873Z (over 1 year ago)
- Language: JavaScript
- Size: 160 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# getp
Get nested properties.
Support dots and array descriptions.
## Example
```javascript
var getp = require('getp');
```
```
o expr value
--------------- ------------------------------- ---------
o.u = 1 getp(o, 'u') 1
o.v = null getp(o, 'v') null
o.z = undefined getp(o, 'z') undefined
o.w.x = 1 getp(o, 'w', 'x') 1
o.w.x = 1 getp(o, ['w', 'x']) 1
o.w.x = 1 getp(o, 'w.x') 1
o.v = null getp(o, 'v.x') undefined
o.x.y.z.u.v = 1 getp(o, 'x', ['y', 'z'], 'u.v') 1
o["a.b"] = 1 getp(o, 'a.b') undefined
o["a.b"] = 1 getp(o, 'a.b', false) 1
```
### getp(obj, name1, name2,..., [parseDots = true])
#### obj
Type: `Anything`
The host object of the property being accessed.
#### name
Type: `String`, `Number`, `Array`
If `Array`, it is expanded as an array of strings.
If `String`, it will be parsed as part of the path to reach the property.
#### parseDots
Type: `Boolean`
Default: `true`
If `false`, name strings will be treated literaly.
### set(o, ...names, v)
```javascript
var set = require('getp/set')
var o = {}
set(o, 'x', 'y', 1)
console.log(o)
// { x: { y: 1 } }
var o = {}
set(o, ['x', 'y'], 1)
console.log(o)
// { x: { y: 1 } }
```