2016-07-22 55 views
0

從Datatables數據我需要外推列數據進行統計計算,如平均值(平均值,中位數,模式)。即使過濾表格,自動計算也必須進行。我設法得到所需的值,但我無法從沒有財產的對象推斷趨勢。例如:如何在沒有jQuery屬性的對象中取值

sortFilteredDataColumn: 
2,2,2,2,2,2,2,2,2,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,5,5,5,5,5,5 

Average: 3.2857142857142856 
Mode -> Object {2: 9, 3: 13, 4: 7, 5: 6} 

好吧,注意模式是[3:13]。所以我想採取這個價值,並增強一個變量,並以便捷的方式顯示它。

FF f5 
    Object { 2=9, 3=13, 4=7, other elements...} 

和DOM ispector:

object 
    2 9 
    3 13 
    4 7 
    5 6 

Desiderable變量中:var myMode = objectValueForIndex:1 任何幫助在很大程度上是理解。

我的代碼:

 "footerCallback": function (row, data, start, end, display) { 
    // console.log('data'+ data.a3 /*JSON.stringify(data)*/); 

     var api = this.api(), data; 

     // Remove the formatting to get integer data for summation 
     var intVal = function (i) { 
      return typeof i === 'string' ? 
       i.replace(/[\$,]/g, '')*1 : 
       typeof i === 'number' ? 
        i : 0; 
     }; 

     // Total over all pages 
     total = api 
      .column(4) 
      .data() 
      .reduce(function (a, b) { 
       return intVal(a) + intVal(b); 
      }, 0); 

     // Total over this page 
     pageTotal = api 
      .column(4, { page: 'current'}) 
      .data() 
      .reduce(function (a, b) { 
       return intVal(a) + intVal(b); 
      }, 0); 


      // SUM COLUMN 4 
      sum = api 
      .column(4, {'search': 'applied'}) 
      .data() 
      .reduce(function (a, b) { 
       return intVal(a) + intVal(b); 
      }, 0); 
      console.log('SUM', sum); 

      var intVal2 = function (i) { 
      return typeof i === 'string' ? 
       i.replace(/[\$,]/g, '')*1 : 
       typeof i === 'number' ? 
        i : 0; 
     }; 

      filteredData = api 
      .column(4, {'search': 'applied'}) 
      .data(function (i, item) { 
      //console.log('filterData',item[ 0 ]); 
       return item;    
      }); 

      var columnStat = new Array(); 
      $.each(filteredData, function(i, item) { 
       // console.log('DataX ',item); 
       columnStat.push(item); 
      }); 
      console.log('columnStat.count: '+columnStat.length); 
      console.log('filteredDataX', columnStat); 

      function compareNumbers2(a, b) 
      { 
       return a - b; 
      } 

      columnStat.sort(compareNumbers2); 
      console.log('sortFilterDataX: '+columnStat); 
      console.log('Media: '+ sum/columnStat.length); 

      var obj2 = { }; 
      for (var i = 0, j = columnStat.length; i < j; i++) { 
      obj2[columnStat[i]] = (obj2[columnStat[i]] || 0) + 1; 
      } 
      console.log(obj2); 

      // Try to Get Value From Object 
      var array = $.map(obj2, function(value, index) { 
      return [value]; 
      }); 
      console.log(array); 

輸出[9,13,7,6]

  // Try to Get key From Object 
      var array2 = $.map(obj2, function(key, index) { 
      return [index]; 
      }); 
      console.log(array2);    

輸出[ 「2」, 「3」, 「4」, 「5」]

  console.log('BREAK LOG'); 

     // Update footer 
     $(api.column(4).footer()).html(
      'M2 '+pageTotal /end +' ('+ total +' totali)' 
     ); 
    } 

回答

0

很簡單:

var min=null, max=null, mod=null; 

      $.each(obj2, function(key, value, index) { 
       var id = parseInt(this.value, 10); 
       if ((min===null) || (value < min)) { min = value; } 
       if ((max===null) || (value > max)) { max = value, mod = key; } 

      }); 
      console.log({moda:mod, fqz:max});