2016-09-13 53 views
0

我似乎無法一起計算一些數字。當我想用:在jQuery數組中計數

for (var key in week_total) { 
    $("." + key).html(week_total[key]); 
} 

我得到一個NaN和的thereforce每個組有一個NaN。這是甚至使用week_total[group] = week_total[group] + work_minutes;的正確方法嗎?

我的腳本:

drawCallback: function (settings) { 
    var api = this.api(), data; 
    var rows = api.rows({page: 'current'}).nodes(); 
    var last = null; 

    var week_total = new Array; 

    api.column(weekColumn, { page: 'current' }).data().each(function (group, i) { 
     var work_hours = api.column(hourColumn).data()[i].split(':'); 
     var work_minutes = (+ work_hours[0]) * 60 + (+ work_hours[1]); 

     week_total[group] = week_total[group] + work_minutes; 

     if (last !== group) { 
      $(rows).eq(i).before('<tr><td colspan="5">{{ trans('admin.worktime.field.week') }} ' + group + '</td><td colspan="3" style="font-style:italic;" class="' + group + '"></td></tr>'); 

      last = group; 
     } 
    }); 

    for (var key in week_total) { 
     $("." + key).html(week_total[key]); 
    } 

    var array = { 
     work_month: work_month, 
     work_year: work_year 
    }; 

    history.pushState(null, null, '?' + $.param(array)); 

    drawDataTable(this); 
} 
+1

哪裏旅遊week_total在week_total [組]初始化,哪些不確定? – Sabik

+0

@Sabik你是什麼意思? 'week_total'被初始化爲'var week_total = new Array;',並且已經用在for(var key in week_total){'part。 –

回答

1

我認爲,問題是,在沒有初始化數組week_total。

計算應該像

week_total[group] = week_total[group] 
    ? week_total[group] + work_minutes 
    : work_minutes; 

如果week_total [組]是不確定的,然後week_total [組] + 1將爲NaN

+0

謝謝你解釋爲什麼我有NaN。 –