https://github.com/tiaanduplessis/set-nested-prop
Set nested object property (supports custom seperator and mutation)
https://github.com/tiaanduplessis/set-nested-prop
dot-notation nested object property set setter
Last synced: 6 months ago
JSON representation
Set nested object property (supports custom seperator and mutation)
- Host: GitHub
- URL: https://github.com/tiaanduplessis/set-nested-prop
- Owner: tiaanduplessis
- License: mit
- Created: 2017-05-28T20:13:32.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2019-06-02T22:38:17.000Z (over 6 years ago)
- Last Synced: 2025-07-29T19:44:56.379Z (6 months ago)
- Topics: dot-notation, nested, object, property, set, setter
- Language: JavaScript
- Size: 70.3 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# set-nested-prop
[](https://npmjs.org/package/set-nested-prop)
[](https://npmjs.org/package/set-nested-prop)
[](https://github.com/RichardLitt/standard-readme)
[](https://npmjs.org/package/set-nested-prop)
[](http://makeapullrequest.com)
> Set nested object property (supports custom seperator, mutation, forcing creation & multi set)
## Table of Contents
- [Install](#install)
- [Usage](#usage)
- [Contribute](#contribute)
- [License](#License)
## Install
This project uses [node](https://nodejs.org) and [npm](https://www.npmjs.com).
```sh
$ npm install set-nested-prop
$ # OR
$ yarn add set-nested-prop
```
## Usage
```js
import set from 'set-nested-prop'
// By default the original object is not modified and . is used as a seperator
const obj1 = {
foo: {
bar: {
baz: 5
}
}
}
const newObj = set(obj1, 'foo.bar.baz', 6)
console.log(newObj) // { foo: { bar: { baz: 6 } } }
console.log(obj1) // { foo: { bar: { baz: 5 } } }
// You can opt in for mutating
const obj2 = {
foo: 5
}
set(obj2, 'foo', 6, { mut: true })
console.log(obj2.foo) // 6
// A custom seperator can be used
const customObj = set(obj1, 'foo*bar*baz', 7, { sep: '*' })
console.log(customObj) // { foo: { bar: { baz: 7 } } }
const forceObj = {}
const forceObjResult = set(forceObj, 'foo.bar', 1, {force: true})
console.log(forceObjResult) // {foo: {bar: 1}}
```
## Contribute
1. Fork it and create your feature branch: git checkout -b my-new-feature
2. Commit your changes: git commit -am 'Add some feature'
3. Push to the branch: git push origin my-new-feature
4. Submit a pull request
## License
MIT