2015-07-11 56 views
0

我試圖循環訪問「年」數組中的月份數組,因此我可以使用自定義角度過濾器計算每個月的計數。下面,在第一個while 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陣列每年都在印有相同的月計爲數組中的最後一年時除外。

問題: 如何解決這個問題..這個月的覆蓋計數是多少?

任何幫助,即使是一個完全不同的方式,這將不勝感激。

+0

'var year = completeByMonth [j]'似乎是問題 – harishr

回答

0

所以問題解決了。我簡單地去掉了2步驟過程:

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

感謝您的所有答案。

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/

+0

似乎沒有任何效果。 –

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; 

你的問題是沒有關係的角度,而是如何使用Javascript(其變量和工作)。您可以在https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures(「在循環中創建閉包:常見錯誤」一節中瞭解有關此問題的更多信息)。

+0

嘗試過,但沒有成功。 –