【发布时间】:2015-07-11 05:54:57
【问题描述】:
我正在尝试遍历“年”数组中的月份数组,以便可以使用自定义角度过滤器计算每个月的计数。下面,在第一个 while 循环中,我创建了要循环的结构。
while(i < itemYears.length) {
//chuck the null end date. push the year part of the other end dates with months array.
if (itemYears[i].end !== undefined && itemYears[i].end !== null) {
completeByMonth.push({ year: itemYears[i].end.substring(0,4), months: months });
}
i++; //increment
}
上述循环创建的结构:
{
year: 2015,
months: [
{ number: '01', text: 'January', count: 0 }
...
...
]
}
在while循环中,我循环遍历每个项目的每个月。
//iterate throught the years
while(j < completeByMonth.length) {
var year = completeByMonth[j], k = 0;
//iterate through the months for year.
while(k < year.months.length) {
year.months[k].count = $filter('endedMonth')(items, year.year, year.months[k].number).length; //filter items on year and month.
console.log(year.year, year.months[k].text, 'count', year.months[k].count);
k++; //increment
}
console.log(year);
j++; //increment
}
console.log('completeByMonth', completeByMonth);
return completeByMonth;
当我运行它时,console.logs 会输出每年每个月的正确计数,除非每年打印的最终 completeByMonth 数组与数组中最后一年的月份计数相同。
问题: 我该如何阻止这个.. 覆盖月数?
任何帮助,即使是完全不同的方式,将不胜感激。
【问题讨论】:
-
var year = completeByMonth[j]似乎是问题
标签: javascript arrays angularjs loops