Skip to content

Instantly share code, notes, and snippets.

@beeblebrox3
Created February 17, 2015 20:29
Show Gist options
  • Save beeblebrox3/4ff35562466806174a97 to your computer and use it in GitHub Desktop.
Save beeblebrox3/4ff35562466806174a97 to your computer and use it in GitHub Desktop.
/**
* 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