[英]Iterate over array of objects
I have a JSON string like this 我有一个像这样的JSON字符串
var json = '{ "Comments":
[
{ "Id" : 1,"Comment" : "Test comment","Name" : "Yogesh","Child" : 0},
{ "Id" : 2,"Comment" : "Test comment II","Name" : "Yogesh","Child" : 0}
]
}';
and I'm trying to iterate over the objects as such: 并且我试图像这样遍历对象:
var parsedJSON = $.parseJSON(json);
var html = "";
for (comment in parsedJSON.Comments) {
html += "Id: " + comment.Id;
html += "Comment: " + comment.Comment;
html += "Name: " + comment.Name;
html += "Child: " + comment.Child;
html += "<br/>";
}
But here comment
in for loop becomes 0
and 1
only, I mean not an object but just a string, how can I iterate over this array? 但是在这里for循环中的
comment
仅变为0
和1
,我的意思不是对象,而是一个字符串,如何遍历此数组?
var json = '{ "Comments": [{ "Id" : 1,"Comment" : "Test comment","Name" : "Yogesh","Child" : 0},{ "Id" : 2,"Comment" : "Test comment II","Name" : "Yogesh","Child" : 0}] }';
var parsedJSON = $.parseJSON(json), // jsonData should json
html = "",
comments =parsedJSON.Comments; // keeping reference of parsedJSON and its an Array
// Here key will give 0, 1 etc ie. index of array
for (var key in comments) {
html += "Id: " + comments[key].Id;
html += "Comment: " + comments[key].Comment;
html += "Name: " + comments[key].Name;
html += "Child: " + comments[key].Child;
html += "<br/>";
}
You seem to have misunderstood how for..in
loops work. 您似乎误解了
for..in
循环的工作方式。 comment
will iteratively be the keys of the array. comment
将迭代地成为数组的键。 In any case, you should not use for..in
on an array, only objects - and even then with caution. 在任何情况下,都不应在数组上仅使用
for..in
对象,即使这样做也要格外小心。
var l = parsedJSON.Comments.length, i, comment;
for( i=0; i<l; i++) {
comment = parseJSON.Comments[i];
// do stuff with comment
}
YOu can try this: 您可以尝试以下方法:
var JsonData = { "Comments":
[
{ "Id" : 1,"Comment" : "Test comment","Name" : "Yogesh","Child" : 0},
{ "Id" : 2,"Comment" : "Test comment II","Name" : "Yogesh","Child" : 0}
]
};
var html = "";
for (var i = 0; i < JsonData.Comments.length; i++) {
comment = JsonData.Comments[i];
html += "Id: " + comment.Id;
html += " Comment: " + comment.Comment;
html += " Name: " + comment.Name;
html += " Child: " + comment.Child;
html += "<br/>";
}
alert(html);
You can use $.each
: 您可以使用
$.each
:
var html = "";
$.each(parsedJSON.Comments, function(i, comment) {
html += "Id: " + comment.Id;
html += "Comment: " + comment.Comment;
html += "Name: " + comment.Name;
html += "Child: " + comment.Child;
html += "<br/>";
}
Alternatively, you could use $.map
: 或者,您可以使用
$.map
:
var html = $.map(parsedJSON.Comments, function(comment, i) {
return "Id: " + comment.Id +
"Comment: " + comment.Comment +
"Name: " + comment.Name +
"Child: " + comment.Child +
"<br/>";
}).join('');
Comments holds an array with two elements. 注释包含一个包含两个元素的数组。
{ "Comments" : [ { 1 }, { 2 }] ...
So you can access it with 因此您可以使用
for (var i = 0, len = parsedJSON.Comments.length; i < len; i++) {
html += "Id: " + parsedJSON.Comments[i].Id;
html += "Comment: " + parsedJSON.Comments[i].Comment;
html += "Name: " + parsedJSON.Comments[i].Name;
html += "Child: " + parsedJSON.Comments[i].Child;
html += "<br/>";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.