fetch-dot
Version:
A pure function that allows you to retrieve a member of an object or an array utilizing dot notation
23 lines (19 loc) • 553 B
JavaScript
;
module.exports = function fetchDot(notation, obj) {
return notation.split(/[\.\[]/g).reduce(function (lastObj, currentProp) {
try {
// Arrays! Tricky tricky. Though not really.
if(currentProp.indexOf(']') !== -1) {
currentProp = parseInt(currentProp.match(/([0-9]+)\]/)[1]);
}
if(lastObj.hasOwnProperty(currentProp) || (Array.isArray(lastObj) && lastObj[currentProp])) {
return lastObj[currentProp];
}
if (currentProp === '') {
return lastObj;
}
} catch(ex) {
return undefined;
}
}, obj);
};