Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

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",
         }
      ]
   }
}
share|improve this question
up vote 1 down vote accepted

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);
}
share|improve this answer
    
thanks ! It works... – 1033 Jul 25 at 4:13
  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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.