Last active
January 13, 2018 21:11
-
-
Save umar-siddiqui/93fe8c47554cd2fc1e04c33c55822801 to your computer and use it in GitHub Desktop.
Array of any elements to string
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 x = [ | |
'1', | |
12, | |
[1, 2, { a: 'z' }], | |
{ a: 'zzz' } | |
] | |
function ArrayToString(x) { | |
var result = '[ ' | |
for(var i = 0; i < x.length; i++) { | |
if(x[i].constructor === Object) { | |
result = result + ObjectToString(x[i]) | |
} else if(x[i].constructor === Array) { | |
result = result + ArrayToString(x[i]) | |
} else if(x[i].constructor === Function) { | |
result = result + x[i].toString() | |
} else { | |
result = result + x[i] | |
} | |
if(x.length != i + 1) result = result + ', ' | |
} | |
result = result + ' ]' | |
return result; | |
} | |
function ObjectToString(x) { | |
var result = '{ ' | |
for(var i in x) { | |
if(x[i].constructor === Object) { | |
result = result + i + ': ' + ObjectToString(x[i]) | |
} else if(x[i].constructor === Array) { | |
result = result + i + ': ' + ArrayToString(x[i]) | |
} else if(x[i].constructor === Function) { | |
result = result + i + ': ' + x[i].toString() | |
} else { | |
result = result + i + ': ' + x[i] | |
} | |
result = result + ', ' | |
} | |
result = result + ' }' | |
return result; | |
} | |
ArrayToString(x) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment