I have json data like this:
{
"Adobe Photoshop": "70",
"Adobe Premier": "71",
"Animasyon": "72",
"AutoCAD": "73",
"Bro\u01开发者_开发技巧5f\u00fcr & Bilet": "75",
"CorelDraw": "76",
"Flash": "77",
"Grafik Tasar\u0131m": "68",
"Karikat\u00fcr": "78",
"Kartvizit Tasar\u0131m": "79",
"Logo Tasar\u0131m": "80",
"Poster": "81",
"Reklam Tasar\u0131m": "74",
"Ses D\u00fczenleme": "82",
"Video D\u00fczenleme": "69",
"Web Tasar\u0131m": "83"
}
First think(Adobe Photoshop) means tag name, second thing(70) means tag id. I want to use this information in jquery like array to build checkboxes for every tag. How can I done this ? Thanks
You're looking for a for in
loop, like this:
for(var key in something) {
if (!something.hasOwnProperty(key)) continue; //Skip inherited members
var value = something[key];
//Do things...
}
Note that the iteration order is not defined.
Something like this?
http://jsfiddle.net/LKNKB/
var data = {"Adobe Photoshop":"70","Adobe Premier":"71","Animasyon":"72","AutoCAD":"73","Bro\u015f\u00fcr & Bilet":"75","CorelDraw":"76","Flash":"77","Grafik Tasar\u0131m":"68","Karikat\u00fcr":"78","Kartvizit Tasar\u0131m":"79","Logo Tasar\u0131m":"80","Poster":"81","Reklam Tasar\u0131m":"74","Ses D\u00fczenleme":"82","Video D\u00fczenleme":"69","Web Tasar\u0131m":"83"};
var $div = $('<div/>');
for(var name in data) {
$('<input/>',{ type:"checkbox", value:data[name] }).appendTo($div);
$('<label/>',{ text:name }).appendTo($div);
$('<br/>').appendTo($div);
}
$div.appendTo('body');
You want a for...in statement:
var i;
for (i in hash) {
doSomething(i, hash[i]);
}
精彩评论