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"
}
]
}
]
}
]
};
就像@Matti说的,使用一些循环:
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/有几种不同的方法来解决这个问题。
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;
}
}
简单、直接。但是将来你可能会想要扩展它,然后过一段时间你就会得到三层循环。这对一些人来说很有趣。我喜欢易读性。这段代码稍微慢一点,但可以说更容易阅读。
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));
每个函数都有自己的作用域,所以不需要担心意外使用"i"两次或类似的事情。它通常更容易阅读——你可以使用更详细的函数名来描述每个函数在做什么。