just-safe-get
Part of a library of zero-dependency npm modules that do just do one thing.
Guilt-free utilities for every occasion.
Try it now
import get from 'just-safe-get';
const obj = {a: {aa: {aaa: 2}}, b: 4};
get(obj, 'a.aa.aaa');
get(obj, ['a', 'aa', 'aaa']);
get(obj, 'b.bb.bbb');
get(obj, ['b', 'bb', 'bbb']);
get(obj.a, 'aa.aaa');
get(obj.a, ['aa', 'aaa']);
get(obj.b, 'bb.bbb');
get(obj.b, ['bb', 'bbb']);
get(null, 'a')
get(undefined, 'a')
get(obj.b, 'bb.bbb', 5);
get(obj.b, ['bb', 'bbb'], true);
get(null, 'a', 5)
get(undefined, 'a', {a: 3})
const obj = {a: {}};
const sym = Symbol();
obj.a[sym] = 4;
get(obj.a, sym);