node package manager
Stop wasting time. Easily manage code sharing in your team. Create a free org ยป



easy dot-notation accessor functions


$ npm install dotmap


Have you ever wanted to build some functions, say, for an Array#map iterator, to reach into an object? Now you can!

var dot = require('dotmap')

var people = [
  {name: {first: 'A', last: 'Turing'}, age: 23},
  {name: {first: 'B', last: 'Cool'}, age: 31},
  {name: {first: 'C', last: 'Coolidge'}, age: 104}

var firsts ='name.first'))
// => ['A', 'B', 'C']

Sometimes you'd rather get undefined then a ReferenceError. For that, we can use

var people = [
  {name: {first: 'A', last: 'Turing'}, age: 23},
  {name: {first: 'B', last: 'Cool'}, age: 31},
  {name: {first: 'C', last: 'Coolidge'}, age: 104},
  {name: {last: 'Roosevelt'}, age: 49}

var firsts ='name.first'))
// => ['A', 'B', 'C', undefine]

And for convenience, you can call dot.get to create and use the accessor function at the same time:

var data = {name: {first: 'C', last: 'Coolidge'}, age: 104}

dot.get(data, 'name.first')
// => 'C'

Note that due to the overhead of creating functions, it's best to avoid using dot.get in a tight loop.

hasn't this been done?

Yes. You've written it. Also, I've used and enjoyed @rauchg's dot-component. The advantage here is that it's special cased for accessing, and it's optimized for speed. We unroll some of the loops to avoid as much array manipulation as possible.

running the tests

change to package root director

$ npm test




MIT. (c) 2013 Agile Diagnosis, see