循环内的循环似乎用最终结果覆盖所有结果

时间:2015-07-11 05:54:58

标签: javascript arrays angularjs loops

我正在尝试在'年'数组中循环一个月的数组,因此我可以使用自定义角度过滤器计算每个月的计数。下面,在第一个单个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数组与数组中的最后一年具有相同的月份数。

问题: 我如何阻止这个...覆盖月数?

任何帮助,即使采用完全不同的方式,都会受到赞赏。

3 个答案:

答案 0 :(得分:0)

我不完全确定过滤器应该做什么。但由于我们处于角度,我已经重写了你的代码以使用forEach,并用示例计数替换了过滤器。

对我来说,这段代码更具可读性,我希望它能解决您的问题。

var i = 0;
angular.forEach(completeByMonth, function(year){
    angular.forEach(year.months, function(month){
        i++;
        month.count = i;  
    });
});

https://jsfiddle.net/HB7LU/15162/

答案 1 :(得分:0)

试试这个:

    while(j < completeByMonth.length) {
        var year = completeByMonth[j], k = 0;
        //iterate through the months for year.
        while(k < year.months.length) {
          (function(month, currentYear){
            currentYear.months[month].count = $filter('endedMonth')(items, currentYear.year, currentYear.months[month].number).length; //filter items on year and month.
            console.log(currentYear.year, currentYear.months[month].text, 'count', currentYear.months[month].count);
          )(k, year);
          k++; //increment
        }
        console.log(year);
        j++; //increment
    }

    console.log('completeByMonth', completeByMonth);
    return completeByMonth;

您的问题与Angular无关,但与javascript(及其变量如何工作)有关。您可以在https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures了解有关该问题的更多信息(第34节;在循环中创建闭包:常见错误&#34;)。

答案 2 :(得分:0)

问题解决了。我只是摆脱了以下两步的过程:

  1. 创建对象。
  2. 计算计数。
  3. 现在我计算对象创建过程中的计数并获得所有年份的正确结果。代码看起来并不好看,但至少它可行。

    while(i--) {
                //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: [
                { number: '01', text: 'January', count: $filter('endedMonth')(items, itemYears[i].end.substring(0,4), '01').length  },
                { number: '02', text: 'February', count: $filter('endedMonth')(items, itemYears[i].end.substring(0,4), '02').length },
                { number: '03', text: 'March', count: $filter('endedMonth')(items, itemYears[i].end.substring(0,4), '03').length },
                { number: '04', text: 'April', count: $filter('endedMonth')(items, itemYears[i].end.substring(0,4), '04').length },
                { number: '05', text: 'May', count: $filter('endedMonth')(items, itemYears[i].end.substring(0,4), '05').length },
                { number: '06', text: 'June', count: $filter('endedMonth')(items, itemYears[i].end.substring(0,4), '06').length },
                { number: '07', text: 'July', count: $filter('endedMonth')(items, itemYears[i].end.substring(0,4), '07').length },
                { number: '08', text: 'August', count: $filter('endedMonth')(items, itemYears[i].end.substring(0,4), '08').length },
                { number: '09', text: 'September', count: $filter('endedMonth')(items, itemYears[i].end.substring(0,4), '09').length },
                { number: '10', text: 'October', count: $filter('endedMonth')(items, itemYears[i].end.substring(0,4), '10').length },
                { number: '11', text: 'November', count: $filter('endedMonth')(items, itemYears[i].end.substring(0,4), '11').length },
                { number: '12', text: 'December', count: $filter('endedMonth')(items, itemYears[i].end.substring(0,4), '12').length }
            ] });
                }
            }
    

    感谢所有答案。