javascript 迭代对象数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11701628/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Iterate over array of objects
提问by yogi
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?
但是在这里comment
for 循环变成0
并且1
只是,我的意思不是一个对象而是一个字符串,我如何迭代这个数组?
回答by thecodeparadox
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/>";
}
回答by Niet the Dark Absol
You seem to have misunderstood how for..in
loops work. comment
will iteratively be the keys of the array. In any case, you should not use for..in
on an array, only objects - and even then with caution.
您似乎误解了for..in
循环的工作原理。comment
将迭代地成为数组的键。在任何情况下,您都不应该for..in
在数组上使用,而只能在对象上使用 - 即使这样也要小心。
var l = parsedJSON.Comments.length, i, comment;
for( i=0; i<l; i++) {
comment = parseJSON.Comments[i];
// do stuff with comment
}
回答by M. Abbas
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);
回答by Engineer
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('');
回答by Gerard Sexton
Comments holds an array with two elements.
Comments 包含一个包含两个元素的数组。
{ "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/>";
}