【问题标题】:Loop within a loop appears to overwrite all results with the final result循环内的循环似乎会用最终结果覆盖所有结果
【发布时间】: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


【解决方案1】:

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

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

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

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

【讨论】:

  • 似乎没有任何作用。
【解决方案2】:

试试这个:

    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(“在循环中创建闭包:一个常见错误”部分)了解有关该问题的更多信息。

【讨论】:

  • 尝试过,但没有成功。
【解决方案3】:

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

  1. 创建对象。
  2. 计算计数。

现在我计算对象创建期间的计数并获得所有年份的正确结果。代码看起来不太好,但至少它可以工作。

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 }
        ] });
            }
        }

感谢大家的回答。

【讨论】:

    猜你喜欢
    • 2019-12-19
    • 2015-10-05
    • 1970-01-01
    • 2021-01-08
    • 2021-03-18
    • 2018-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多