Easily use dot notation to get, or set a property of a nested object. A Node.js library.
Create the nested chain of objects with set and dot notation with one simple statement.
const objectd = require('object-dot')
console.log(
objectd.set({ object: {}, path: 'a.b.c', value: 'foo' })
)
// => { a: { b: { c: 'foo' } } }
// Array of the property chain will work too!
console.log(
objectd.set({ object: {}, path: ['a', 'b', 'c'], value: 'foo' })
)
// Alternatively you may use arguments as parameters instead of an object.
console.log(
objectd.set({}, 'a.b.c', 'foo')
)Get the value of a nested chain of objects without checking each object in the chain for its existence.
const objectd = require('object-dot')
// when one of the properties in the chain is undefined. Safely return undefined.
let object = { foo: { bar: 'you!' }}
console.log(
objectd.get({ object, path: 'foo.bar.c.d'})
)
//=> undefined
// return a default value if property is undefined
object = { foo: { bar: 'you!' }}
console.log(
objectd.get({ object, path: 'foo.bar.c.d', value: 'my default value'})
)
//=> 'my default value'
// When the property exist.
object = { a: { foo: { bar: 'you!' } }}
console.log(
objectd.get({ object, path: 'a.foo'})
)
//=> { bar: 'you!' }$ npm install object-dot --saveISC