Question

var data = {
    "name": "nba",
        "conference": [
        {
            "name": "Eastern",
            "divisions": [
                {
                    "name": "Atlantic",
                    "teams": [
                        {
                            "location": "Brooklyn",
                            "name": "Nets"
                        },
                        {
                            "location": "Boston",
                            "name": "Celtics"
                        },
                        {
                            "location": "New York",
                            "name": "Knicks"
                        },
                        {
                            "location": "Philadelphia",
                            "name": "76ers"
                        },
                        {
                            "location": "Toronto",
                            "name": "Raptors"
                        }
                    ]
                },
                {
                    "name": "Central",
                    "teams": [
                        {
                            "location": "Chicago",
                            "name": "Bulls"
                        },
                        {
                            "location": "Cleveland",
                            "name": "Cavaliers"
                        },
                        {
                            "location": "Detroit",
                            "name": "Pistons"
                        },
                        {
                            "location": "Indiana",
                            "name": "Pacers"
                        },
                        {
                            "location": "Milwaukee",
                            "name": "Bucks"
                        }
                    ]
                },
                {
                    "name": "Southeast",
                    "teams": [
                        {
                            "location": "Atlanta",
                            "name": "Hawks"
                        },
                        {
                            "location": "Charlotte",
                            "name": "Bobcats"
                        },
                        {
                            "location": "Miami",
                            "name": "Heat"
                        },
                        {
                            "location": "Orlando",
                            "name": "Magic"
                        },
                        {
                            "location": "Washington",
                            "name": "Wizards"
                        }
                    ]
                }
            ]
        },
        {
            "name": "Western",
            "divisions": [
                {
                    "name": "Northwest",
                    "teams": [
                        {
                            "location": "Denver",
                            "name": "Nuggets"
                        },
                        {
                            "location": "Minnesota",
                            "name": "Timberwolves"
                        },
                        {
                            "location": "Oklahoma City",
                            "name": "Thunder"
                        },
                        {
                            "location": "Portland",
                            "name": "Trailblazers"
                        },
                        {
                            "location": "Utah",
                            "name": "Jazz"
                        }
                    ]
                },
                {
                    "name": "Pacific",
                    "teams": [
                        {
                            "location": "Golden State",
                            "name": "Warriors"
                        },
                        {
                            "location": "Los Angeles",
                            "name": "Clippers"
                        },
                        {
                            "location": "Los Angeles",
                            "name": "Lakers"
                        },
                        {
                            "location": "Phoenix",
                            "name": "Suns"
                        },
                        {
                            "location": "Sacramento",
                            "name": "Kings"
                        }
                    ]
                },
                {
                    "name": "Southwest",
                    "teams": [
                        {
                            "location": "Dallas",
                            "name": "Mavericks"
                        },
                        {
                            "location": "Houston",
                            "name": "Rockets"
                        },
                        {
                            "location": "Memphis",
                            "name": "Grizzlies"
                        },
                        {
                            "location": "New Orleans",
                            "name": "Pelicans"
                        },
                        {
                            "location": "San Antonio",
                            "name": "Spurs"
                        }
                    ]
                }
            ]
        }
    ]
};
Was it helpful?

Solution

Like @Matti said, use some loops:

var i = 0,
  j = 0,
  k = 0,
  conference,
  division,
  teamCount = 0,
  teams = [];

for(i = 0; i < data.conference.length; i++) {
  conference = data.conference[i];
  for(j = 0; j < conference.divisions.length; j++) {
    division = conference.divisions[j];
    for(k = 0; k < division.teams.length; k++) {
      teamCount += 1;
      teams.push(division.teams[k].name);
    }
  }
}

console.log(teamCount);
console.log(teams.join(','));

http://jsfiddle.net/AMQ8W/2/

OTHER TIPS

There are a few different ways to tackle this.

teamCount = 0;
for(var i = 0, l = data.conference.length; i < l; i++) {
    console.log(division);
    var division = data.conference[i];
    for(var ti = 0, tl = division.divisions.length; ti < tl; ti++) {
        teamCount += division.divisions[ti].teams.length;
    }
}

Simple, straightforward. But you're likely going to want to expand upon it in the future, and then after a while you've ended up with three levels of loops. Which is a lot of fun for some folks. I prefer legibility. This code is a touch slower, but it is arguably easier to read.

function countTeams (data) {

    var teamCount = 0;

    function conferenceIterator (conferences) {
        for(var i = 0, l = conferences.length; i < l; i++) {
            divisionIterator(conferences[i].divisions);
        }
    }

    function divisionIterator (division) {
        for(var i = 0, l = division.length; i < l; i++) {
            teamCount += division[i].teams.length;
        }
    }
    conferenceIterator (data.conference);
    return teamCount;
}

console.log(countTeams(data));

Each of the functions has its own scope, so there's no concerns about accidentally using "i" twice or anything like that. And it's just generally a bit easier to read - you can use more verbose function names to describe what each is doing.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top