R
R
Roman-OverOffers2014-02-14 16:02:24
JavaScript
Roman-OverOffers, 2014-02-14 16:02:24

NaN: how to tidy up data in JS?

Hey!
I'm currently busy implementing the Totals count in dataTables using a function like this.

var stat_options = $.extend({
    "sAjaxSource": "/dashboard/graph/", 
    "aoColumns": [
        { "mData": "date" }, 			
        // {"mData":"clicks"}, 
        { "mData":"unique_clicks" }, 
        { "mData":"events_count" },
        { "mData":"approved" },
        { "mData":"pending" },
        { "mData":"rejected" }, 
        { "mData":"cpc" }, 
        { "mData":"ltr" }, 
        { "mData":"approval_rate" }, 
        // {"mData":"total_revenue"}, 
        // {"mData":"rejected_revenue"},
        { "mData":"pending_revenue" },
        { "mData":"approved_revenue" }  
    ], 
    "fnFooterCallback": function( nRow, aaData, iStart, iEnd, aiDisplay ) {
                var iClicks = 0
                var iConv = 0
                var iPendingRevenue = 0
                var iApprovedRevenue = 0
                for ( var i=0 ; i<aaData.length ; i++ )
                {
                    iClicks += aaData[i][1]*1;
                    iConv += aaData[i][2]*1;
                    iPendingRevenue += aaData[i][9]*1;
                    iApprovedRevenue += aaData[i][10]*1;
                }
             
                var nCells = nRow.getElementsByTagName('th');
                nCells[1].innerHTML = parseInt(iClicks);
                nCells[2].innerHTML = parseInt(iConv);
                nCells[9].innerHTML = parseInt(iPendingRevenue);
                nCells[10].innerHTML = parseInt(iApprovedRevenue);
    }
}, tables_options);

I'm trying to count in Totals:
unique_clicks, events_count, pending_revenue, approved_revenue.
JSON response format in terms of numbers.
"unique_clicks": "1001",
"events_count": 38,
"pending_revenue": 1000.0,
"approved_revenue": 1500.0
if there is no data, outputs 0 everywhere
How to convert them to numeric?
In JS, I am a complete zero.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
A
alekstsk, 2014-02-14
@Roman-OverOffers

Since you have such a patchy response, parseInt needs every element in the loop. Now you are already parsing a string, glued together from all elements.
-------------------------------------------------- -----------
Sorry, the first time I looked inattentively, the cast to number is already in the loop.
-piece of aaData, then the problem is that this is an object containing objects, you are trying to sort through it as an array containing objects with arrays. and it doesn't even have a length field.

for (i in aaData){
   iClicks += parseInt(i['unique_clicks'], 10);
   ........
}

like this is how i see it

N
Nazar Mokrinsky, 2014-02-14
@nazarpc

Perhaps they will help parseInt()you parseFloat()?

R
Roman-OverOffers, 2014-02-14
@Roman-OverOffers

Response example
"2013-06-17": {"payout": "7900.000000", "ltr": "2.9277", "pending_revenue": 1500.0, "leads": "32", "events_count": 38, "cpc" : "7.23", "rejected": 3, "approved_revenue": 7900.0, "unique_clicks": "1093", "date": "2013-06-17", "approval_rate": 84, "approved": 32, " pending": 3, "rejected_revenue": 1000.0},

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question