0

main.php jQuery code:

$.getJSON('posts.php',function(data){
    data.posts.forEach(function(post){
        // set variables and append divs to document
    })
    data.comments.forEach(function(post){
        // set variables and append divs to document
    })
})

(Old - Works with current jQuery code)

Example object containing 2 posts and 3 comments. Post with id: 5 has 1 comment and post id: 2 has 2 comments.

// the two posts ID: 5 and 2

{"posts":[{
    "id":"5",
    "image":"link.jpg",
    "submitter":"4322309",
    "views":"3"
},
{
    "id":"2",
    "image":"link.jpg",
    "submitter":"4322309",
    "views":"10"
}],

// now each comment tied to the posts

"comments":[
{
    "id":"1",
    "submitter":"submitter",
    "time":"2435657",
    "comment":"comment",
    "score":"10",
    "postid":"2"
},
{
    "id":"2",
    "submitter":"submitter",
    "time":"2435657",
    "comment":"comment",
    "score":"10",
    "postid":"2"
},
{
    "id":"3",
    "submitter":"submitter",
    "time":"2435657",
    "comment":"comment",
    "score":"10",
    "postid":"5"
}]}

(NEW - Does not work with current jQuery code)

Example object containing 2 posts and 3 comments. Post with id: 5 has 1 comment and post id: 2 has 2 comments.

// the two posts ID: 5 and 2

{ 
    "posts":{
        "5": {
            "id":"5",
            "image":"link.jpg",
            "submitter":"4322309",
            "views":"3"
        },
        "2": {
            "id":"2",
            "image":"link.jpg",
            "submitter":"4322309",
            "views":"5"
        }
    },

    // now each comment tied to the posts
    "comments":{
        "2": [{
            "id":"1",
            "submitter":"submitter",
            "time":"2435657",
            "comment":"comment",
            "score":"10",
            "postid":"2"
        },
        {
            "id":"2",
            "submitter":"submitter",
            "time":"2435657",
            "comment":"comment",
            "score":"10",
            "postid":"2"
        }
    ],
        "5": [{
            "id":"3",
            "submitter":"submitter",
            "time":"2435657",
            "comment":"comment",
            "score":"10",
            "postid":"5"
        }]
    }
}

I'm not sure how to use this JSON object in this new scenario.

Basically how do I loop through this new one?

Pankaj
  • 7,908
  • 6
  • 42
  • 65
frosty
  • 2,779
  • 6
  • 34
  • 63

4 Answers4

2

You can easily iterate like this:-

$.each(data['posts'], function(outerKey, idVal) {  //outerKyeas are 2, 5
   $.each(idVal, function(innerKey, val) {   // innerKeys are id,submitter etc
     console.log(innerKey, val);
  });
});

Comments can be looped through like the same way.

sharif2008
  • 2,716
  • 3
  • 20
  • 34
1

First option (vanilla JS):

var postObj;
for (var id in data.posts) {
  postObj = data.posts[id];
  // do your thing
}

var commentList;
for (var id in data.comments) {
  commentList = data.comments[id];
  commentList.forEach(function(comment) {
    // do your thing
  });
}

For more info on for...in loops https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in


Second Option (jQuery):

$.each(data.posts, function(id, post) {
  // do your thing
});

$.each(data.comments, function(id, commentList) {
  $.each(commentList, function(index, comment) {
    // do your thing. you could also use the forEach loop if you want
  });   
});

For more info on $.each http://api.jquery.com/jquery.each/

akosel
  • 1,183
  • 9
  • 14
0

Try $.each coupled with a basic 'for (var post in data.posts)' loop:

var data = {
  "posts": {
    "5": {
      "id": "5",
      "image": "link.jpg",
      "submitter": "4322309",
      "views": "3"
    },
    "2": {
      "id": "2",
      "image": "link.jpg",
      "submitter": "4322309",
      "views": "5"
    }
  },

  // now each comment tied to the posts
  "comments": {
    "2": [{
      "id": "1",
      "submitter": "submitter",
      "time": "2435657",
      "comment": "comment",
      "score": "10",
      "postid": "2"
    }, {
      "id": "2",
      "submitter": "submitter",
      "time": "2435657",
      "comment": "comment",
      "score": "10",
      "postid": "2"
    }],
    "5": [{
      "id": "3",
      "submitter": "submitter",
      "time": "2435657",
      "comment": "comment",
      "score": "10",
      "postid": "5"
    }]
  }
};


for (var post in data.posts) {
  $.each(data.comments[data.posts[post].id], function() {
    alert("Post: " + data.posts[post].id + ", Comment ID: " + this.id + ', Comment Text: "' + this.comment + '"')
  });
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
omikes
  • 8,064
  • 8
  • 37
  • 50
0

Well I say you need 3 loops here to fetch each comment object like

DEMO

$(data.comments).each(function(index,commentArray){ 
    $.each(commentArray,function(index,value){
        $.each(value,function(index1,value1){
            console.log(value1);
        });
    });
});

for Post you can get it with a single loop

$.each(data.posts,function(index,post){
        console.log(post);
});
Guruprasad J Rao
  • 29,410
  • 14
  • 101
  • 200