Created
February 17, 2015 20:29
-
-
Save beeblebrox3/4ff35562466806174a97 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
/** | |
* Get element from obj by string path | |
* @param {string} path | |
* @param {Object} obj reference object. If not provided or invalid, window will be used | |
* @return {mixed} | |
*/ | |
App.helpers.object.getFlattened = function (path, obj) { | |
"use strict"; | |
if (typeof path !== "string") { | |
throw "path must be string"; | |
} | |
if (obj instanceof Object === false) { | |
obj = window; | |
} | |
path = path.split('.'); | |
var i, | |
size = path.length, | |
response = obj; | |
for (i = 0; i < size; i += 1) { | |
if (response instanceof Object === false) { | |
return null; | |
} | |
if (response.hasOwnProperty(path[i])) { | |
response = response[path[i]]; | |
} else { | |
return null; | |
} | |
} | |
return response; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment