luis c7750ca5f1 ready to start? | 5 年之前 | |
---|---|---|
.. | ||
index.js | 5 年之前 | |
license | 5 年之前 | |
package.json | 5 年之前 | |
readme.md | 5 年之前 |
Get, set, or delete a property from a nested object using a dot path
$ npm install --save dot-prop
const dotProp = require('dot-prop');
// getter
dotProp.get({foo: {bar: 'unicorn'}}, 'foo.bar');
//=> 'unicorn'
dotProp.get({foo: {bar: 'a'}}, 'foo.notDefined.deep');
//=> undefined
dotProp.get({foo: {'dot.dot': 'unicorn'}}, 'foo.dot\\.dot');
//=> 'unicorn'
// setter
const obj = {foo: {bar: 'a'}};
dotProp.set(obj, 'foo.bar', 'b');
console.log(obj);
//=> {foo: {bar: 'b'}}
dotProp.set(obj, 'foo.baz', 'x');
console.log(obj);
//=> {foo: {bar: 'b', baz: 'x'}}
// deleter
const obj = {foo: {bar: 'a'}};
dotProp.delete(obj, 'foo.bar');
console.log(obj);
//=> {foo: {}}
obj.foo.bar = {x: 'y', y: 'x'};
dotProp.delete(obj, 'foo.bar.x');
console.log(obj);
//=> {foo: {bar: {y: 'x'}}}
Type: Object
Object to get, set, or delete the path
value.
Type: string
Path of the property in the object, using .
to separate each nested key.
Use \\.
if you have a .
in the key.
Type: any
Value to set at path
.
MIT © Sindre Sorhus