npm install dottie
For detailed usage, check source or tests.
Gets nested value, or undefined if unreachable, or a default value if passed.
var values = {
some: {
nested: {
key: 'foobar';
}
}
}
dottie.get(values, 'some.nested.key'); // returns 'foobar'
dottie.get(values, 'some.undefined.key'); // returns undefined
dottie.get(values, 'some.undefined.key', 'defaultval'); // returns 'defaultval'
Sets nested value, creates nested structure if needed
dottie.set(values, 'some.nested.value', someValue);
Transform object from keys with dottie notation to nested objects
var values = {
'user.name': 'Gummy Bear',
'user.email': '[email protected]',
'user.professional.title': 'King',
'user.professional.employer': 'Candy Mountain'
};
var transformed = dottie.transform(values);
/*
{
user: {
name: 'Gummy Bear',
email: 'gummybear@candymountain.com',
professional: {
title: 'King',
employer: 'Candy Mountain'
}
}
}
*/
var values = {
'user_name': 'Mick Hansen',
'user_email': '[email protected]'
};
var transformed = dottie.transform(values, { delimiter: '_' });
/*
{
user: {
name: 'Mick Hansen',
email: 'maker@mhansen.io'
}
}
*/