prop
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]) ⇒
Any
prop.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
const objA = a: b: 'c' prop// > 'c' // Specify a value to return if a property is not foundprop// > nullprop// > 24 // Traverse an arrayconst objB = a: b: 'c' prop// > 'c'
Object
| Array
prop.set(obj, propString, value, [loose]) ⇒ 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
const objA = a: b: 'c' const updatedA = prop// > objA == { a: { b: 'c' } }// > updatedA == { a: { b: 'c', c: 'd' } } const constructedObj = prop// > constructedObj == { a: [{ b: { c: 12 } }] }
set.mutate(obj, propString, value, [loose])
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
const objA = a: b: 'c' propset// > objA == { a: { b: 'c', c: 'd' } } const emptyObj = {}propset// > emptyObj == { a: [{ b: { c: 12 } }] }
Object
| Array
prop.merge(obj, propString, value, [loose]) ⇒ 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
const objA = a: b: 'c' const updatedA = prop// > objA == { a: { b: 'c' } }// > updatedA == { a: { b: 'c', d: 'e', f: 'g' } } const objB = a: 0 1 2 const updatedB = prop// > objB == { a: [0, 1, 2] }// > updatedB == { a: [0, 1, 3, 4] }
merge.mutate(obj, propString, value, [loose])
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
const objA = a: b: 'c' propmerge// > objA == { a: { b: 'c', d: 'e', f: 'g' } } const objB = a: 0 1 2 propmerge// > objB == { a: [0, 1, 3, 4] }
Boolean
prop.has(obj, propString) ⇒ 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
const objA = a: b: c: 'd' prop// > falseprop// > true
Object
| Array
prop.del(obj, propString) ⇒ 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
const objA = a: b: c: 'd' const updatedA = prop// > objA == { a: { b: { c: 'd' } } }// > updatedA == { a: {} }
del.mutate(obj, propString)
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
const objA = a: b: c: 'd' propdel// nooppropdel// objA == { a: [{}] }