2016-07-22 5 views
0

Von Datatables Daten muss ich Spalten Daten für eine statistische Berechnung wie Mittelwerte (Mittelwert, Median, Modus) extrapolieren. Die automatische Berechnung muss auch dann stattfinden, wenn die Tabelle gefiltert ist. Ich habe es geschafft, die gewünschten Werte zu erhalten, aber ich kann den Trend von einem Objekt ohne Eigenschaft nicht extrapolieren. Ex:Wie nehme ich einen Wert in einem Objekt ohne Eigenschaften in 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} 

Ok Hinweis, dass der Modus [: 13 3] ist. Also möchte ich diesen Wert nehmen und eine Variable verbessern und sie auf eine bequeme Weise anzeigen.

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

und in DOM ispector:

object 
    2 9 
    3 13 
    4 7 
    5 6 

Desiderable Variable: var myMode = objectValueForIndex: 1 Jede Hilfe ist weitgehend anerkannt.

mein Code:

 "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); 

Ausgang [9, 13, 7, 6]

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

output [ "2", "3", "4", "5"]

  console.log('BREAK LOG'); 

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

Antwort

0

war einfach:

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}); 
Verwandte Themen