javascript - Loop within a loop appears to overwrite all results with the final result -


i'm trying loop through array of months within array of 'years' can calculate count each month using custom angular filter. below, in first single while loop create structure i'm going loop through.

while(i < itemyears.length) {                 //chuck null end date. push year part of other end dates months array.                 if (itemyears[i].end !== undefined && itemyears[i].end !== null) {                     completebymonth.push({ year: itemyears[i].end.substring(0,4), months: months });                 }                 i++; //increment             } 

structure above loop creates:

 {   year: 2015,   months: [    { number: '01', text: 'january', count: 0 }     ...     ...   ] } 

in while within while loop, loop through each month of each item.

        //iterate throught years         while(j < completebymonth.length) {             var year = completebymonth[j], k = 0;             //iterate through months year.             while(k < year.months.length) {                 year.months[k].count = $filter('endedmonth')(items, year.year, year.months[k].number).length; //filter items on year , 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; 

when run console.logs output correct counts each month of each year, except when final completebymonth array printed every year has same month counts final year in array.

question: how stop this.. overwriting of month counts?

any help, entirely different way of doing it, appreciated.

i not entirely sure filter supposed doing. since in angular, have rewritten code use foreach, , replaced filter example count.

to me code more readable, , hope solves issue.

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

https://jsfiddle.net/hb7lu/15162/


Comments

Popular posts from this blog

java - Andrioid studio start fail: Fatal error initializing 'null' -

android - Gradle sync Error:Configuration with name 'default' not found -

StringGrid issue in Delphi XE8 firemonkey mobile app -