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

2 Answers 2

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);
}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.