1.0.5 • Published 9 months ago
@dmc-towns/object-methods v1.0.5
Javascript Object Methods
A pair of methods used to manipulate nested objects
Installation
npm install @dmc-towns/object-methods
Getting & setting properties
import { setPropertyByAddress, getPropertyByAddress } from '@dmc-towns/object-methods'
const obj = {}
setPropertyByAddress(obj, 'a.b.c', 3)
console.log(obj)
// Output: { a: { b: { c: 3 } } }
let value = getPropertyByAddress(obj, 'a.b.c')
console.log(value)
// Output: 3
Using a custom delimiter
setPropertyByAddress(obj, 'a/b/c', 4, '/')
console.log(obj)
// Output: { a: { b: { c: 4 } } }
Specifying a fallback value
let value = getPropertyByAddress(obj, 'a.b.d')
console.log(obj)
// Output: null
value = getPropertyByAddress(obj, 'a.b.d', 1)
console.log(obj)
// Output: 1