2

Here's my json file, I am trying to access the properties inside the group such as homeTeam and markerImage.

And how do determine the number of groups inside the competition. Please help!

{
   "competition":{
      "group1":[
         {
            "homeTeam":"Lawrence Library",
            "markerImage":"images/red.png",
         },
         {
            "homeTeam":"Hamilton Library",
            "markerImage":"images/white.png",

         },
         {
            "homeTeam":"Applebees",
            "markerImage":"images/newcastle.png",

         }
      ],
      "group2":[
         {
            "homeTeam":"Lawrence Library",
            "markerImage":"images/red.png",

         },
         {
            "homeTeam":"Hamilton Library",
            "markerImage":"images/white.png",

         },
         {
            "homeTeam":"Applebees",
            "markerImage":"images/newcastle.png",
         }
      ]
   }
}
1033
  • 61
  • 1
  • 9

2 Answers2

1

To determine the number of groups in competition object you can use

Object.keys(obj.competition).length

console.log(Object.keys(obj.competition).length);

And to loop trought your groups try something like this

for(var x in Object(obj.competition.group1)){
    var newObj = Object(obj.competition.group1[x]);
    console.log(newObj.homeTeam + ' and '+ newObj.markerImage);
}
Mario Rozic
  • 254
  • 5
  • 12
0
  1. Parse your JSON object and store it in a javascript object
  2. Use underscore and lo-Dash libraries to calculate number of objects

Link to calculate it with Underscore and Lo-Dash libraries

Community
  • 1
  • 1
Adeel Ilyas
  • 437
  • 3
  • 11