1. es5-dot-prop
Get, set, or delete a property from a nested object using a dot path
es5-dot-prop
Package: es5-dot-prop
Created by: sindresorhus
Last modified: Sun, 01 May 2022 11:27:45 GMT
Version: 4.1.1
License: MIT
Downloads: 42
Repository: https://github.com/sindresorhus/dot-prop

Install

npm install es5-dot-prop
yarn add es5-dot-prop

dot-prop Build Status

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

Install

$ npm install --save 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'}}

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)

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

Dependencies

RELATED POST

10 Must-Know Windows Shortcuts That Will Save You Time

10 Must-Know Windows Shortcuts That Will Save You Time

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Arrays vs Linked Lists: Which is Better for Memory Management in Data Structures?

Navigating AWS Networking: Essential Hacks for Smooth Operation

Navigating AWS Networking: Essential Hacks for Smooth Operation

Achieving Stunning Visuals with Unity's Global Illumination

Achieving Stunning Visuals with Unity's Global Illumination

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code

Nim's Hidden Gems: Lesser-known Features for Writing Efficient Code