https://github.com/strikeentco/set
Set nested values on an object using a dot path or custom separator
https://github.com/strikeentco/set
deep dot dot-path dot-prop fast javascript lodash lodash-set nested-value nodejs object object-path path set set-deep small value
Last synced: 7 months ago
JSON representation
Set nested values on an object using a dot path or custom separator
- Host: GitHub
- URL: https://github.com/strikeentco/set
- Owner: strikeentco
- License: mit
- Created: 2018-03-18T18:20:45.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2023-03-04T02:36:25.000Z (almost 3 years ago)
- Last Synced: 2024-11-01T17:47:01.824Z (over 1 year ago)
- Topics: deep, dot, dot-path, dot-prop, fast, javascript, lodash, lodash-set, nested-value, nodejs, object, object-path, path, set, set-deep, small, value
- Language: JavaScript
- Homepage:
- Size: 350 KB
- Stars: 3
- Watchers: 2
- Forks: 1
- Open Issues: 6
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
set [](https://github.com/strikeentco/set/blob/master/LICENSE) [](https://www.npmjs.com/package/@strikeentco/set)
==========
[](https://travis-ci.org/strikeentco/set) [](https://www.npmjs.com/package/@strikeentco/set) [](https://codeclimate.com/github/strikeentco/set/test_coverage)
One of the smallest (*31 sloc*) and most effective implementations of setting a nested value on an object.
# Usage
```sh
$ npm install @strikeentco/set --save
```
```javascript
const set = require('@strikeentco/set');
set({ a: { b: 'c' } }, 'a.b', 'd');
//=> { a: { b: 'd' } }
set({ a: { b: ['c', 'd'] } }, 'a.b.1', 'e');
//=> { a: { b: ['c', 'e'] } }
set({ a: { b: ['c', 'd'] } }, ['a', 'b'], 'c');
//=> { a: { b: 'c' } }
set({ a: { b: 'c' } }, 'a.b.c.d', 'e');
//=> { a: { b: { c: { d: 'e' } } } }
set({ a: { b: 'c' } }, 'a:b', 'd', ':');
//=> { a: { b: 'd' } }
```
## API
### set(obj, path, val, [separator])
#### Params:
* **obj** (*Object*) - Source object.
* **path** (*String|Array*) - String or array with path.
* **val** (*Any*) - Value to set.
* **[separator]** (*String*) - `.` by default.
## License
The MIT License (MIT)
Copyright (c) 2018-present Alexey Bystrov