如何迭代JSON结构?我有以下JSON结构:[{ "id":"10", "class": "child-of-9" }, { "id": "11", "classd": "child-of-10" }]如何使用JavaScript迭代它?
3 回答
qq_花开花谢_0
TA贡献1835条经验 获得超7个赞
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));});
一只甜甜圈
TA贡献1836条经验 获得超5个赞
var arr = [ {"id":"10", "class": "child-of-9"}, {"id":"11", "classd": "child-of-10"}];
for (var i = 0; i < arr.length; i++){
var obj = arr[i];
for (var key in obj){
var attrName = key;
var attrValue = obj[key];
}
}
var arr = [ {"id":"10", "class": "child-of-9"}, {"id":"11", "class": "child-of-10"}];
for (var i = 0; i < arr.length; i++){
document.write("<br><br>array index: " + i);
var obj = arr[i];
for (var key in obj){
var value = obj[key];
document.write("<br> - " + key + ": " + value);
}
}
守候你守候我
TA贡献1802条经验 获得超10个赞
<html><body><script type="text/javascript">var mycars = [{name:'Susita'}, {name:'BMW'}];for (i in mycars){ document.write(mycars[i].name + "<br />");}</script></body></html>
SusitaBMW
添加回答
举报
0/150
提交
取消