Created
December 11, 2015 19:46
-
-
Save patrickml/94a3f1119cf4c1b8af28 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_.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