npm install prop-ops
prop-ops
assists in performing CRUD operations on javascript objects and arrays.
By default, prop-ops
does not mutate objects, but offers the option of doing so.
Authors: Matthew Meyers, Sunyoung Kim
License: MIT
- prop
- .get(obj, propString, [fallBack]) ⇒
Any
- .set(obj, propString, value, [loose]) ⇒
Object
|Array
- .merge(obj, propString, value, [loose]) ⇒
Object
|Array
- .has(obj, propString) ⇒
Boolean
- .del(obj, propString) ⇒
Object
|Array
- .get(obj, propString, [fallBack]) ⇒
Safely access deeply nested properties of unstructured objects
Kind: static method of prop
Param | Type | Default | Description |
---|---|---|---|
obj | Object | Array |
the object or array to traverse | |
propString | String |
the path to the desired property | |
[fallBack] | Any |
|
a fall back value to return if the property is not found |
Example
import * as prop from 'prop-ops'
const objA = { a: { b: 'c' } }
prop.get(objA, 'a.b')
// > 'c'
// Specify a value to return if a property is not found
prop.get(objA, 'a.nope')
// > null
prop.get(objA, 'a.nope', 24)
// > 24
// Traverse an array
const objB = { a: [{ b: 'c' }] }
prop.get(objB, 'a.[0].b')
// > 'c'
Sets deeply nested object properties.
Kind: static method of prop
Returns: Object
| Array
- an updated version of obj
Param | Type | Default | Description |
---|---|---|---|
obj | Object | Array |
the object or array to traverse | |
propString | String |
the path to the desired property | |
value | Any |
the value to set | |
[loose] | Boolean |
false |
create new objects / arrays along the path if undefined is encountered |
Example
import * as prop from 'prop-ops'
const objA = { a: { b: 'c' } }
const updatedA = prop.set(objA, 'a.c', 'd')
// > objA == { a: { b: 'c' } }
// > updatedA == { a: { b: 'c', c: 'd' } }
const constructedObj = prop.set({}, 'a.[0].b.c', 12, true)
// > constructedObj == { a: [{ b: { c: 12 } }] }
Like set
, but will modify the original object
Kind: static method of set
Param | Type | Default | Description |
---|---|---|---|
obj | Object | Array |
the object or array to traverse | |
propString | String |
the path to the desired property | |
value | Any |
the value to set | |
[loose] | Boolean |
false |
create new objects / arrays along the path if undefined is encountered |
Example
import * as prop from 'prop-ops'
const objA = { a: { b: 'c' } }
prop.set.mutate(objA, 'a.c', 'd')
// > objA == { a: { b: 'c', c: 'd' } }
const emptyObj = {}
prop.set.mutate(emptyObj, 'a.[0].b.c', 12, true)
// > emptyObj == { a: [{ b: { c: 12 } }] }
Merge deeply nested objects or arrays.
Kind: static method of prop
Returns: Object
| Array
- an updated version of obj
Param | Type | Default | Description |
---|---|---|---|
obj | Object | Array |
the object or array to traverse | |
propString | String |
the path to the desired property | |
value | Object | Array |
the object to merge | |
[loose] | Boolean |
false |
create new objects / arrays along the path if undefined is encountered |
Example
import * as prop from 'prop-ops'
const objA = { a: { b: 'c' } }
const updatedA = prop.merge(objA, 'a', { d: 'e', f: 'g' })
// > objA == { a: { b: 'c' } }
// > updatedA == { a: { b: 'c', d: 'e', f: 'g' } }
const objB = { a: [0, 1, 2] }
const updatedB = prop.merge(objB, 'a', [, , 3, 4])
// > objB == { a: [0, 1, 2] }
// > updatedB == { a: [0, 1, 3, 4] }
Like merge
, but will modify the original object
Kind: static method of merge
Param | Type | Default | Description |
---|---|---|---|
obj | Object | Array |
the object or array to traverse | |
propString | String |
the path to the desired property | |
value | Object | Array |
the object to merge | |
[loose] | Boolean |
false |
create new objects / arrays along the path if undefined is encountered |
Example
import * as prop from 'prop-ops'
const objA = { a: { b: 'c' } }
prop.merge.mutate(objA, 'a', { d: 'e', f: 'g' })
// > objA == { a: { b: 'c', d: 'e', f: 'g' } }
const objB = { a: [0, 1, 2] }
prop.merge.mutate(objB, 'a', [, , 3, 4])
// > objB == { a: [0, 1, 3, 4] }
Check if an object or array has a property
Kind: static method of prop
Param | Type | Description |
---|---|---|
obj | Object |
object to traverse |
propString | String |
the path to the desired property |
Example
import * as prop from 'prop-ops'
const objA = { a: [{ b: { c: 'd' } }] }
prop.has(objA, 'a.b')
// > false
prop.has(objA, 'a.[0].b.c')
// > true
Deletes deeply nested object properties
Kind: static method of prop
Param | Type | Description |
---|---|---|
obj | Object | Array |
object to traverse |
propString | String |
the path to the desired property |
Example
import * as prop from 'prop-ops'
const objA = { a: { b: { c: 'd' } } }
const updatedA = prop.del(objA, 'a.b')
// > objA == { a: { b: { c: 'd' } } }
// > updatedA == { a: {} }
Like del
, but will modify the original object
Kind: static method of del
Param | Type | Description |
---|---|---|
obj | Object | Array |
object to traverse |
propString | String |
the path to the desired property |
Example
import * as prop from 'prop-ops'
const objA = { a: [{ b: { c: 'd' } }] }
prop.del.mutate(objA, 'a.b')
// noop
prop.del.mutate(objA, 'a.[0].b')
// objA == { a: [{}] }