猿问

如何迭代JSON结构?

如何迭代JSON结构?

我有以下JSON结构:

[{ "id":"10", "class": "child-of-9" }, { "id": "11", "classd": "child-of-10" }]

如何使用JavaScript迭代它?


胡子哥哥
浏览 562回答 3
3回答

qq_花开花谢_0

摘自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));});

一只甜甜圈

var arr = [ {"id":"10", "class": "child-of-9"}, {"id":"11", "classd": "child-of-10"}];for (var i = 0; i < arr.length; i++){&nbsp; &nbsp; var obj = arr[i];&nbsp; &nbsp; for (var key in obj){&nbsp; &nbsp; &nbsp; &nbsp; var attrName = key;&nbsp; &nbsp; &nbsp; &nbsp; var attrValue = obj[key];&nbsp; &nbsp; }}var arr = [ {"id":"10", "class": "child-of-9"}, {"id":"11", "class": "child-of-10"}];&nbsp; &nbsp;&nbsp;for (var i = 0; i < arr.length; i++){&nbsp; document.write("<br><br>array index: " + i);&nbsp; var obj = arr[i];&nbsp; for (var key in obj){&nbsp; &nbsp; var value = obj[key];&nbsp; &nbsp; document.write("<br> - " + key + ": " + value);&nbsp; }}注意:对于简单对象来说,for-in方法是很酷的。与DOM对象一起使用并不是很明智。

守候你守候我

使用前程:<html><body><script&nbsp;type="text/javascript">var&nbsp;mycars&nbsp;=&nbsp;[{name:'Susita'},&nbsp;{name:'BMW'}];for&nbsp;(i&nbsp;in&nbsp;mycars){ &nbsp;&nbsp;document.write(mycars[i].name&nbsp;+&nbsp;"<br&nbsp;/>");}</script></body></html>将导致:SusitaBMW
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答