Skip to content
This repository has been archived by the owner on Dec 19, 2023. It is now read-only.

418sec/set-deep-prop

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

set-deep-prop

Sets the value for a deeply nested objects and arrays.

Example

const setDeepProp = require('set-deep-prop')

const obj = {
  order: {
    number: 123,
    items: [
      {
        sku: 1,
        description: 'shirt',
      },
      {
        sku: 2,
        description: 'pants'
      }
    ]
  }
}

setDeepProp(obj, ['order', 'items', 1, 'description'], 'hat')
console.log(obj.order.items[1].description) // 'hat'

See test/set-deep-prop.test.js for more examples.

Installation

npm install --save set-deep-prop

API

setDeepProp(obj, path, value)
@param {object} obj -- A deeply nested object
@param {array} path -- The path to the object as an array. Ex: ['topkey', 'nextkey', 0, 'nestedKey']
@param {any} value -- The new value to set.

@return {object} -- The updated object. Note that this method mutates the original object.

Test

npm test

License

MIT

About

Sets a deeply nested object's value

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%