Lodash.get - ish
by jonahe
JavaScript
const get = path => obj => {
const pathArr = path.split('.');
return pathArr.reduce((soFar, nextPath) => {
if(typeof soFar === 'undefined') return soFar;
if(typeof soFar[nextPath] === 'undefined') return undefined;
return soFar[nextPath];
}, obj);
}
const nested = { a: { b: { c: { d: ['index 0', 'index 1'] } }}};
console.log(get('a.b.c.d.1')(nested))
const nestedArr = [1,2,[11,22, [{ a: ['hello', ', world!'] }, 111]]];
console.log(get('2.2.0.a.0')(nestedArr) + get('2.2.0.a.1')(nestedArr))