Get, set, or delete a property from a nested object using a dot path
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.editorconfig Meta tweaks Dec 31, 2017
.gitattributes Require Node.js 6 Jun 20, 2018
.gitignore Meta tweaks Dec 31, 2017
.npmrc Meta tweaks Dec 31, 2017
.travis.yml Update benchmark (#49) Jun 20, 2018
bench.js Require Node.js 6 Jun 20, 2018
index.js Return object from the `.set()` method (#42) Jul 24, 2017
license Meta tweaks Dec 31, 2017
package.json Require Node.js 6 Jun 20, 2018
readme.md Meta tweaks Dec 31, 2017
test.js Meta tweaks Dec 31, 2017

readme.md

dot-prop Build Status

Get, set, or delete a property from a nested object using a dot path

Install

$ npm install dot-prop

Usage

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: {bar: 'a'}}, 'foo.notDefined.deep', 'default value');
//=> 'default value'

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'}}

const foo = dotProp.set({}, 'foo.bar', 'c');
console.log(foo);
//=> {foo: {bar: 'c'}}

dotProp.set(obj, 'foo.baz', 'x');
console.log(obj);
//=> {foo: {bar: 'b', baz: 'x'}}

// Has
dotProp.has({foo: {bar: 'unicorn'}}, 'foo.bar');
//=> true

// 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'}}}

API

get(obj, path, [defaultValue])

set(obj, path, value)

Returns the object.

has(obj, path)

delete(obj, path)

obj

Type: Object

Object to get, set, or delete the path value.

path

Type: string

Path of the property in the object, using . to separate each nested key.

Use \\. if you have a . in the key.

value

Type: any

Value to set at path.

defaultValue

Type: any

Default value.

License

MIT © Sindre Sorhus