Created
May 10, 2014 21:02
-
-
Save DirkyJerky/11ab15230253794e3de8 to your computer and use it in GitHub Desktop.
Create heirarchy of javascript properties in JSON (call via listPropertiesOf(aJSObject, depthLimit))
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
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