Created
March 14, 2011 17:34
-
-
Save namusyaka/869512 to your computer and use it in GitHub Desktop.
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
function parseObject(obj) { | |
var _d = document, create = function(element) {return _d.createElement(element)} | |
switch(obj.constructor) { | |
case Array: | |
var list = create('ul'); | |
break | |
case Object: | |
var list = create('dl'); | |
break | |
default: | |
return _d.createTextNode(obj) | |
} | |
switch(list.tagName) { | |
case 'UL': | |
for(var i in obj) { | |
if(obj[i] instanceof Object) | |
list.appendChild(create('li')).appendChild(arguments.callee(obj[i])) | |
else | |
list.appendChild(create('li')).innerHTML = obj[i]; | |
} | |
break | |
case 'DL': | |
for(var i in obj) { | |
list.appendChild(create('dt')).innerHTML = i; | |
if(obj[i] instanceof Object) | |
list.appendChild(create('dd')).appendChild(arguments.callee(obj[i])); | |
else | |
list.appendChild(create('dd')).innerHTML =obj[i] | |
} | |
break | |
} | |
return list | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment