Friday, September 11, 2009

[JavaScript] for in 을 써먹어보자.

//json 형식으로 키와 값으로 배열을 만들어 놓자..
var tmpObj = [{"name":"red", "code":"#FF0000"}, {"name":"blue", "code":"#0000FF"}, {"name":"green", "code":"#00FF00"}];

var result="";
for(var i = 0; i< tmpObj.length; i++){
for(var obj in tmpObj[i]){
result+=("\n key :" + obj + ", value " + tmpObj[i][obj]);
}
result+="\n";
}
alert(result);

//출력결과
---------------------------
Windows Internet Explorer
---------------------------

key :name, value red
key :code, value #FF0000

key :name, value blue
key :code, value #0000FF

key :name, value green
key :code, value #00FF00

---------------------------
확인
---------------------------