propper
v1.0.4
Published
A tool for getting and setting object properties using dot notation, path notation, and arrays to speficy properties.
Downloads
678
Readme
propper
A tool for getting and setting object properties using dot notation, path notation, and arrays to speficy properties.
Installation
npm install propper
Usage
var prop = require('propper')
var obj = {a: 1, b: {c: 'hello'}};
// getters
console.log(prop(obj, 'b.c')); // 'hello'
console.log(prop(obj, '/b/c')); // 'hello'
console.log(prop(obj, ['b', 'c'])); // 'hello'
// setters
console.log(prop(obj, 'b.c', 1)); // 1
console.log(prop(obj, '/b/c', 2)); // 2
console.log(prop(obj, ['b', 'c'], 3)); // 3
// setters can also be used to delete keys from an object by passing in undefined as the value
prop(obj, 'b.c', undefined);
console.log(obj); // {a: 1}
// If the deletion of a key would yield, an empty object, then that object will be removed too.
obj = {a: {b: {c: true}}};
prop(obj, 'a.b.c', undefined);
console.log(obj); // {}