题意:
我如何遍历一个 JSON 结构?
问题背景:
I have the following JSON structure:
我有以下 JSON 结构:
[{ "id":"10", "class": "child-of-9" }, { "id": "11", "classd": "child-of-10" }]
How do I iterate over it using JavaScript?
我如何使用 JavaScript 遍历它?
问题解决:
Taken from jQuery docs:
摘自 jQuery 文档:
var arr = [ "one", "two", "three", "four", "five" ];
var obj = { one:1, two:2, three:3, four:4, five:5 };
jQuery.each(arr, function() {
$("#" + this).text("My id is " + this + ".");
return (this != "four"); // will stop running to skip "five"
});
jQuery.each(obj, function(i, val) {
$("#" + i).append(document.createTextNode(" - " + val));
});