Skip to content

Non-destructively set a nested property, given its path

Notifications You must be signed in to change notification settings

micro-js/set-prop

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

set-prop

Build status Git tag NPM version Code style

Non-destructively set a nested property, given its path

Installation

$ npm install @f/set-prop

Usage

var setProp = require('@f/set-prop')
var obj = {a: {b: 1}}
var newObj = setProp('a.b', obj, 2)

obj.a.b === 1
newObj.a.b === 2
obj !== newObj

API

setProp(path, obj, value)

  • path - The path (specified as array or dotted string) of the property you wish to set
  • obj - The root object you want to set within
  • value - The value you want to assign to path within obj

Returns: A new root obj that has path changed to value, and all of the sub-objects in its tree cloned as well.

License

MIT

About

Non-destructively set a nested property, given its path

Resources

Stars

Watchers

Forks

Packages

No packages published