Exception-free nested nullable attribute accessor. An alternative to facebookincubator/idx in 57 bytes.
Just copy/paste this function into your project:
var mb=(f,...p)=>(o,i=o[f])=>i&&p[0]!=null?mb(...p)(i):i
Alternatively, you can download mb.js.
var getHello = mb('a', 'b', 0, 'hello');
var obj1 = {
a: {
b: [{ hello: 'world' }]
}
};
var obj2 = {
c: {
d: 'e'
}
};
getHello(obj1); // world
getHello(obj2); // undefined
- Clone and shorten current code.
- Please open
test.html
in your browser and open console to see if all the tests pass.