Created
October 14, 2017 14:02
-
-
Save FrankSpierings/65e9d74736e2b2dd27decf02b8119a7c to your computer and use it in GitHub Desktop.
Javascript display an object
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
//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