Skip to content

Instantly share code, notes, and snippets.

@DirkyJerky
Created May 10, 2014 21:02
Show Gist options
  • Save DirkyJerky/11ab15230253794e3de8 to your computer and use it in GitHub Desktop.
Save DirkyJerky/11ab15230253794e3de8 to your computer and use it in GitHub Desktop.
Create heirarchy of javascript properties in JSON (call via listPropertiesOf(aJSObject, depthLimit))
var excluded = [document];
var listPropertiesOf = function(object, maxDepth) {
return listPropertiesOfWDepth(object, 0, maxDepth);
};
var listPropertiesOfWDepth = function(object, curDepth, maxDepth) {
if(object == null) {
return 'null';
}
/*
console.log("listProperties called w/" + object + ', ' + curDepth + ', ' + maxDepth)
*/
var returnString = '';
var commaC = false;
$.each(Object.keys(object), function(K, V) {
if(commaC) {
returnString += (', ');
} else {
commaC = true;
}
returnString += '\n';
for(var i = 0; i < curDepth; i++) {
returnString += ' ';
}
returnString += ('{ "' + V + '" : "');
var obj = object[V];
if(((typeof obj) == 'object') &&
(curDepth < maxDepth) &&
(obj != object) &&
!(excluded.some(
function(v,i,a) {
return (v == object)
}
)
)){
returnString += listPropertiesOfWDepth(obj, (curDepth + 1), maxDepth);
} else {
returnString += obj;
}
returnString += '" }'
});
return returnString;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment