Skip to content

DMCTowns/ObjectMethods

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

A pair of methods used to manipulate Javascript objects

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published