Skip to content

Instantly share code, notes, and snippets.

@crossan007
Created November 5, 2021 15:25
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 crossan007/c0351708632fcba0a52829485bbaff5c to your computer and use it in GitHub Desktop.
Save crossan007/c0351708632fcba0a52829485bbaff5c to your computer and use it in GitHub Desktop.
Render JavaScript object to a string; include all properties and sub-objects.
function showProps(obj, objName) {
var result = ``;
for (var i in obj) {
// obj.hasOwnProperty() is used to filter out properties from the object's prototype chain
if (obj.hasOwnProperty(i)) {
if (typeof obj[i] != "object"){
result += `${objName}.${i} = ${obj[i]}\n`;
}
else {
result += `${objName}.${i} = ${showProps(obj[i],objName+"."+i)}\n`;
}
}
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment