Skip to content Skip to sidebar Skip to footer

Getting Json Object Titles

I've tried searching on here, but I can't figure this out. I'm trying to do an each function that grabs each title of the object, my json is. {'games':{ 'featured': [ {

Solution 1:

You can use nested $.each() calls to iterate each array of objects at category : property name of "games" property of data

var data = {
  "games": {
    "featured": [{
      "game_id": "2"
    }, {
      "game_id": "15",
    }],
    "popular": [{
      "game_id": "2"
    }],
    "new": [{
      "game_id": "2",
    }, {
      "game_id": "15",
    }, {
      "game_id": "1",
    }]
  }
};

$.each(data.games, function(i, category) {
  console.log(i + ":");
  $.each(category, function(key, obj) {
    console.log(obj)
  });
});
<scriptsrc="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Solution 2:

I suppose you want to get featured, popular, new? You already have your answer, The first argument i returns the key of the object, where the second argument returns the value which is category.

    $.each(data.games, function(i, category) {
        // category is what you are looking foralert(JSON.stringify(category));
        //oralert(JSON.stringify(data.games[i]));
    });

Hope that helps

Post a Comment for "Getting Json Object Titles"