0

Edit: My issue is that when giving the function appendAllComments the parameter subid, when I alert subid it says for example 77 which indeed exists within the JSON object. So:

global_save_json.input == global_save_json.subid == global_save_json.77 (?) or "77"

yet it's returning undefined for whatever reason? Is this a number vs. string issue or something?


I need to select a key of the comments object and then loop through the array.

From fresh_posts.php:

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

    "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"
        }]
    }
}

Initially have:

$.getJSON('fresh_posts.php',function(data){
    global_save_json = data.comments;

Now I need to use that global_save_json once again later down the track.

$('.main').on('click', '.new-comments', function(e) {

      var fullid = e.target.id;
      var subid = fullid.substring(8); // subid = number like 75

      function appendAllComments(input) {

        for (var commentList in global_save_json.input) {
          $.each(commentList, function(index, c) {
            // i need to set variables, i.e. var id = the value of "id" key in the object in the array of comments
          })
        }
      }

      appendAllComments(subid);

 })

I've tried to re-use an earlier for loop but it's not working, I'm stuck - see comment inside loop for what I'm trying to do

frosty
  • 2,779
  • 6
  • 34
  • 63
  • `data.comments['2'].forEach(function() {...` – adeneo Jun 11 '15 at 17:56
  • The click function isn't within the `$.getJSON` though – frosty Jun 11 '15 at 18:00
  • What do you mean by "its not working?" Any error messages? It might be an ASYNC problem... maybe trying to click before the data is loaded? Have a look at jQuery promises: [jQuery Promises](https://api.jquery.com/promise/) – Chris Mitchell Jun 11 '15 at 18:07
  • It shouldn't be an async problem as the data is already saved to the `global_save_json` variable. There's no errors, it's just not doing what I want it to do... – frosty Jun 11 '15 at 18:08
  • can you confirm that there is data in the global_save_json? `console.log(global_save_json)` Also, why global_save_json.input? Wouldn't it be just global_save_json? – Chris Mitchell Jun 11 '15 at 18:13
  • There is as I do a promise thing which uses it on load. `.input` is so it can select the key within the `comments` object, right?? – frosty Jun 11 '15 at 18:15
  • Not sure... what if you used this instead: `$.each(global_save_json, function(commentsListIndex, commentsList) { $.each(commentList, function(index, c) { .... }` And your values would be c.id, etc. – Chris Mitchell Jun 11 '15 at 18:18
  • Yeah, I have that as a previous one (append all comments for all posts). For this one I only want to append all comments for a single post (linked by ID). – frosty Jun 11 '15 at 18:26

0 Answers0