Skip to content

robboerman/dottie.js

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

79 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Build Status Bitdeli Badge

Install

npm install dottie

Usage

For detailed usage, check source or tests.

Get value

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'

Set value

Sets nested value, creates nested structure if needed

dottie.set(values, 'some.nested.value', someValue);

Transform object

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

With a custom delimiter

var values = {
  'user_name': 'Mick Hansen',
  'user_email': '[email protected]'
};
var transformed = dottie.transform(values, { delimiter: '_' });

/*
{
  user: {
    name: 'Mick Hansen',
    email: 'maker@mhansen.io'
  }
}
*/

License

MIT

About

Object traversing/manipulation util

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%