Skip to content

Instantly share code, notes, and snippets.

@prettycode
Created June 29, 2016 04:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prettycode/5366e7ffb7cbb32dfecc02c1068b6069 to your computer and use it in GitHub Desktop.
Save prettycode/5366e7ffb7cbb32dfecc02c1068b6069 to your computer and use it in GitHub Desktop.
Given an object, returns a list of property paths that can be used with lodash's _.get()
function discoverPropertyPaths(parentObject, parentPath) {
var paths = [],
propertyNames = Object.keys(parentObject);
propertyNames.forEach(function (propertyName) {
var propertyPath = (parentPath ? (parentPath + '.') : '') + propertyName,
property = parentObject[propertyName];
if (typeof property === 'object') {
var isArray = angular.isArray(property);
paths.push({
id: propertyPath,
type: isArray ? 'array' : 'object'
});
if (isArray) {
property = property[0];
propertyPath += '[]';
}
[].push.apply(paths, discoverPropertyPaths(property, propertyPath));
}
else {
paths.push({
id: propertyPath,
type: property
});
}
});
return paths;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment