Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 循环中的循环显示为用最终结果覆盖所有结果_Javascript_Arrays_Angularjs_Loops - Fatal编程技术网

Javascript 循环中的循环显示为用最终结果覆盖所有结果

Javascript 循环中的循环显示为用最终结果覆盖所有结果,javascript,arrays,angularjs,loops,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 !==

我试图在一个“年”数组中循环一个月数组,这样我就可以使用自定义角度过滤器计算每个月的计数。下面,在第一个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
            }
在whilewithwhile循环中,我循环每个项目的每个月

        //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;
//在这些年中反复
而(j
当我运行console.logs时,它们会输出每年每个月的正确计数,除非每年打印的最终CompletedByMonth数组与数组中的最后一年具有相同的月计数

问题: 我怎样才能阻止这一切。。覆盖月份计数


任何帮助,即使是完全不同的方式,都将不胜感激。

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

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

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

    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;
while(j

您的问题与Angular无关,而是与javascript(及其变量的工作方式)有关。您可以在(在循环中创建闭包:一个常见错误)一节中了解有关该问题的更多信息。

因此问题得到了解决。我只需取消以下两个步骤:

  • 创建对象
  • 计算计数
  • 现在我计算了对象创建期间的计数,并得到了所有年份的正确结果

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

    谢谢你的回答。

    var year=completeByMonth[j]
    问题似乎没有任何效果。尝试过,但没有成功。
    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 }
            ] });
                }
            }