Skip to content
Exception-free nested nullable attribute accessor
JavaScript HTML
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
LICENSE
README.md
mb.js
test.html
test.js

README.md

mb

goat

Exception-free nested nullable attribute accessor. An alternative to facebookincubator/idx in 38 bytes.


Install

Just copy/paste this function into your project:

var mb=p=>o=>p.map(c=>o=(o||{})[c])&&o

Alternatively, you can download mb.js.

Use

var getHello = mb(["a", "b", 0, "hello"]);
var getHelloLength = mb(["a", "b", 0, "hello", "length"]);

var obj1 = {
  a: {
    b: [{ hello: "world" }]
  }
};

var obj2 = {
  c: {
    d: "e"
  }
};

getHello(obj1); // world
getHelloLength(obj1); // 5

getHello(obj2); // undefined
getHelloLength(obj2); // undefined

Contribution and Code-Golfing

  1. Clone and shorten current code.
  2. Please open test.html in your browser and open console to see if all the tests pass.

Contributors

You can’t perform that action at this time.