我如何使用Javascript迭代以下内容来计算团队的数量
How can i iterate over the following using Javascript to count the number of teams?
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"两次或类似的事情。它通常更容易阅读——你可以使用更详细的函数名来描述每个函数在做什么。
相关文章:
- 根据元素和容器大小计算边距
- 从Rally获取一个特定的标记,以便计算另一个字段中的值
- 使用D3.js计算带有字母间距的文本长度
- 使用CSS或JavaScript计算分页符的数量
- 可以't计算自定义谷歌地图的js
- 如何计算每个元素's的高度,并将这些值用作函数中的变量
- JavaScript计算帮助(乘以时间)
- 如何计算对象文字中的键
- JavaScript循环无法正确计算/显示结果
- 与域在同一台计算机上运行的NODEJS服务器的CORS错误
- 四舍五入JavaScript计算
- 计算HTML中的页数
- 使用jQuery计算数组中的对象以更改进度条
- 如何在jquery中使用实时计算求和值
- 计算多个项目的价格
- 计算CSS3缩放框在另一个框中的最高位置
- 如何计算二十面体的法线
- if(foo!==null)的计算结果为true,即使foo为null
- 在Angular中重新使用HTML端的计算文本
- 我如何使用Javascript迭代以下内容来计算团队的数量