Skip to content

Instantly share code, notes, and snippets.

@FrankSpierings
Created October 14, 2017 14:02
Show Gist options
  • Save FrankSpierings/65e9d74736e2b2dd27decf02b8119a7c to your computer and use it in GitHub Desktop.
Save FrankSpierings/65e9d74736e2b2dd27decf02b8119a7c to your computer and use it in GitHub Desktop.
Javascript display an object
//Use this function to show an object's contents.
function showObject(obj) {
var result = null
if (obj && obj.constructor === Array) {
result = []
}
else if (obj === null) {
return null
}
else {
result = {}
}
for (property in obj) {
if (obj.hasOwnProperty(property)) {
var value = obj[property]
if (typeof(value) === 'object' || typeof(value) === 'function') {
if (obj.constructor === Array) {
result.push(showObject(value))
}
else {
result[property] = showObject(value)
}
}
else {
result[property] = value
}
}
}
return result
}
// Grab someObject and display it like below
console.log(JSON.stringify(someObject, null, 4)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment