node package manager
Don’t reinvent the wheel. Reuse code within your team. Create a free org »

object-locator

object-locator

Build Status

This small lib helps a developer to go directly inside object properties for any given depth and to return the datat

The problem

   const obj = {a: {b: {c: 'd'}}};
   
   // get a.b.c - verbose way 
   if(&& a.b && a.c)
     return a.b.c

object-locator solution

   const locator = require('object-locator');
   const obj = {a: {b: {c: 'd'}}};
   
   // elegant way 
   locator(obj).a.b.c.getOrElse('bla') // === 'd' 
   // or for non exist object 
   locator(obj).a.b.not.exist.getOrElse('bla') // === 'bla' 

Install

$ npm install --save-dev object-locator
  • Implemented via ES6 proxy, so it it compatible with node 6 >