Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery: Sum of all data appended with specific date

Tags:

json

jquery

sum

I retrieve some data off an API and I append it as such:

if (jsontext == '[]') {
    return false
} else {

    var newTr = '';
    for (var i = 0; i < json.length; i++) {
        newTr += (' / ' + json[i].date + ' ' + json[i].price);

    }

    console.log(newTr);

}

which gives me a huge list of results as such:

/ 07-09-2012 72.60 / 11-09-2012 194.98 / 03-09-2012 94.82 / 04-09-2012 187.56 / 31-10-2012 72 / 18-09-2012 204.75 / 26-09-2012 243.73 / 14-09-2012 86.40 / 20-09-2012 91.63 / 28-09-2012 96.56 / 01-09-2012 94.62 / 17-09-2012 94.86 / 17-09-2012 83.25 / 12-09-2012 94.85 / 18-09-2012 94.86 / 18-09-2012 68.74 / 21-09-2012 94.86 / 21-09-2012 94.86 / 24-09-2012 144.23 / 28-09-2012 92.77 / 30-09-2012 92.77 / 28-09-2012 92.77 / 13-09-2012 151.12 / 03-09-2012 125.80 / 05-09-2012 92.61 / 05-09-2012 95.54 / 12-09-2012 94.59 / 12-09-2012 94.59 / 13-09-2012 125.83 / 18-09-2012 109.38

and keep's going.

How can I take the sum of the pricing for the month 09?

The date format is dd-mm-yyyy.

like image 980
jQuerybeast Avatar asked Nov 19 '12 17:11

jQuerybeast


2 Answers

I assume that what you have is a string like below,

"/ 07-09-2012 72.60 / 11-09-2012 194.98 / 03-09-2012 94.82 / 04-09-2012 187.56 / 31-10-2012 72 / 18-09-2012 204.75 / 26-09-2012 243.73 / 14-09-2012 86.40 / 20-09-2012 91.63 / 28-09-2012 96.56 / 01-09-2012 94.62 / 17-09-2012 94.86 / 17-09-2012 83.25 / 12-09-2012 94.85 / 18-09-2012 94.86 / 18-09-2012 68.74 / 21-09-2012 94.86 / 21-09-2012 94.86 / 24-09-2012 144.23 / 28-09-2012 92.77 / 30-09-2012 92.77 / 28-09-2012 92.77 / 13-09-2012 151.12 / 03-09-2012 125.80 / 05-09-2012 92.61 / 05-09-2012 95.54 / 12-09-2012 94.59 / 12-09-2012 94.59 / 13-09-2012 125.83 / 18-09-2012 109.38"

If that is the case then you need to parse the string to calculate the sum by month.

DEMO: http://jsfiddle.net/pBNNt/

Full Code:

var resultArr = result.split('/');
var results = {}; //sum by month
for (var i = 0; i < resultArr.length; i++) {    
    if ( resultArr[i].length >= 11) { //it has date
        var resultTkn = resultArr[i].split(' ');

        if (resultTkn[1].length == 10) { //it is a date
            var date = resultTkn[1].split('-');
            var sum = 0;
            if (results.hasOwnProperty(date[1])) {
                sum = results[date[1]];
            }

            sum += parseFloat(resultTkn[2]);
            results[date[1]] = sum;
        }
    }
}

Now you can access the sum by month using the results objects. results["09"] returned 3305.93

like image 188
Selvakumar Arumugam Avatar answered Oct 06 '22 21:10

Selvakumar Arumugam


EDIT: I've tried to explain this as clear as I can based on the comments:

Firstly, you need to add function, which is responsible for parsing your date string (the input parameter in format dd-mm-yyyy), and returns the proper date object:

// parse a date in dd-mm-yyyy
function parseDate(input) {
    var parts = input.match(/(\d+)/g);
    // new Date(year, month [, date [, hours[, minutes[, seconds[, ms]]]]])
    return new Date(parts[2], parts[1] - 1, parts[0]); // months are 0-based
}

After that write function, which does what you've asked about - returns the sum of prices for provided month:

// get sum of prices for given month
function getSumForMonth(data, month) {
    var sum = 0;
    month -= 1; // months are 0-based 
    $.each(data, function (idx, item) {
        if (parseDate(item.date).getMonth() == month) {
            sum += parseFloat(item.price);
        }
    });
    return sum;
}

To get what you want you have to invoke function defined above. The data parameter is your json variable, and month parameter is the index of the month you want to get the result for:

var sum = getSumForMonth(json, 9); // sum for september
console.log(sum);

EDIT 2: Protection in case of null items in deserialized json object...

$.each(data, function(idx, item) {
    if (item != null && item.date != null && item.price != null) {
        if (parseDate(item.date).getMonth() == month) {
            sum += parseFloat(item.price);
        }
    }
});
like image 32
jwaliszko Avatar answered Oct 06 '22 20:10

jwaliszko