Skip to content

Instantly share code, notes, and snippets.

@patrickml
Created December 11, 2015 19:46
Show Gist options
  • Save patrickml/94a3f1119cf4c1b8af28 to your computer and use it in GitHub Desktop.
Save patrickml/94a3f1119cf4c1b8af28 to your computer and use it in GitHub Desktop.
_.mixin({
get: function (obj, key) {
var type = typeof key;
if (type == 'string' || type == "number") {
key = ("" + key).replace(/\[(.*?)\]/,/\[(.*?)\]/, function (m, key) { //handle case where [1] may occur
return '.' + key.replace(/["']/g,/["']/g, ""); //strip quotes
}).split('.');
}
for (var i = 0, l = key.length; i < l; i++) {
if (typeof obj !== 'undefined' && _.has(obj, key[i])) obj = obj[key[i]];
else return undefined;
}
return obj;
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment