最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Iterate over array of objects - Stack Overflow

programmeradmin3浏览0评论

I have a JSON string like this

 var json =  '{ "Comments": 
    [
      { "Id" : 1,"Comment" : "Test ment","Name" : "Yogesh","Child" : 0},
      { "Id" : 2,"Comment" : "Test ment II","Name" : "Yogesh","Child" : 0}
    ] 
    }';

and I'm trying to iterate over the objects as such:

var parsedJSON = $.parseJSON(json);

var html = "";    
for (ment in parsedJSON.Comments) {
  html += "Id: " + ment.Id;
  html += "Comment: " + ment.Comment;
  html += "Name: " + ment.Name;
  html += "Child: " + ment.Child;
  html += "<br/>";
}

But here ment in for loop bees 0 and 1 only, I mean not an object but just a string, how can I iterate over this array?

I have a JSON string like this

 var json =  '{ "Comments": 
    [
      { "Id" : 1,"Comment" : "Test ment","Name" : "Yogesh","Child" : 0},
      { "Id" : 2,"Comment" : "Test ment II","Name" : "Yogesh","Child" : 0}
    ] 
    }';

and I'm trying to iterate over the objects as such:

var parsedJSON = $.parseJSON(json);

var html = "";    
for (ment in parsedJSON.Comments) {
  html += "Id: " + ment.Id;
  html += "Comment: " + ment.Comment;
  html += "Name: " + ment.Name;
  html += "Child: " + ment.Child;
  html += "<br/>";
}

But here ment in for loop bees 0 and 1 only, I mean not an object but just a string, how can I iterate over this array?

Share Improve this question edited Jul 28, 2012 at 14:19 Torsten Walter 5,79224 silver badges26 bronze badges asked Jul 28, 2012 at 13:57 yogiyogi 19.6k13 gold badges64 silver badges93 bronze badges 1
  • 1 You are not iterating over JSON, you are iterating over a JavaScript array. I corrected your question accordingly. You should use a for loop for that. You might also read about arrays in JavaScript. – Felix Kling Commented Jul 28, 2012 at 14:03
Add a ment  | 

5 Answers 5

Reset to default 6
var json = '{ "Comments": [{ "Id" : 1,"Comment" : "Test ment","Name" : "Yogesh","Child" : 0},{ "Id" : 2,"Comment" : "Test ment II","Name" : "Yogesh","Child" : 0}] }';

var parsedJSON = $.parseJSON(json), // jsonData should json
    html = "",
    ments =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 ments) {
    html += "Id: " + ments[key].Id;
    html += "Comment: " + ments[key].Comment;
    html += "Name: " + ments[key].Name;
    html += "Child: " + ments[key].Child;
    html += "<br/>";
}

Demo

You seem to have misunderstood how for..in loops work. ment 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.

var l = parsedJSON.Comments.length, i, ment;
for( i=0; i<l; i++) {
    ment = parseJSON.Comments[i];
    // do stuff with ment
}

YOu can try this:

     var JsonData =  { "Comments":
        [
          { "Id" : 1,"Comment" : "Test ment","Name" : "Yogesh","Child" : 0},
          { "Id" : 2,"Comment" : "Test ment II","Name" : "Yogesh","Child" : 0}
        ]
        };

    var html = "";    
    for (var i = 0; i < JsonData.Comments.length; i++) {
      ment = JsonData.Comments[i];
      html += "Id: " + ment.Id;
      html += " Comment: " + ment.Comment;
      html += " Name: " + ment.Name;
      html += " Child: " + ment.Child;
      html += "<br/>";
    }

alert(html);

You can use $.each:

var html = ""; 
$.each(parsedJSON.Comments, function(i, ment) {
   html += "Id: " + ment.Id;
   html += "Comment: " + ment.Comment;
   html += "Name: " + ment.Name;
   html += "Child: " + ment.Child;
   html += "<br/>";
}

Alternatively, you could use $.map:

var html = $.map(parsedJSON.Comments, function(ment, i) {
   return "Id: " + ment.Id +
          "Comment: " + ment.Comment +
          "Name: " + ment.Name +
          "Child: " + ment.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/>";
}
发布评论

评论列表(0)

  1. 暂无评论