Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loop through array of objects and return sum of each total values by object element id

I'm calculating taxes for a very complicate invoicing approach. I can't explain the whole process but if you have questions I will answer as best as I can.

I come up with an array of objects in JS:

[
 {row_id: "20003859", expense_id: "429", tax_select: "tax1", tax_id: "1", tax_name: "GST 5%", tax_no: "", tax_value: "13.23"}, 
 {row_id: "20003859", expense_id: "429", tax_select: "tax2", tax_id: "3", tax_name: "QST 9.975%", tax_no: "", tax_value: "26.38"}, 
 {row_id: "20003840", expense_id: "409", tax_select: "tax1", tax_id: "1", tax_name: "GST 5%", tax_no: "", tax_value: "13.23"}, 
 {row_id: "20003840", expense_id: "409", tax_select: "tax2", tax_id: "3", tax_name: "QST 9.975%", tax_no: "", tax_value: "26.38"},
 {row_id: "20003870", expense_id: "419", tax_select: "tax1", tax_id: "2", tax_name: "HST 13%", tax_no: "", tax_value: "34.39"}
]

As you can see I have 3 tax_ids: 1, 2 and 3. I can have many but for the sake of simplicity I put only 3.

I need to loop through this array of objects and come up with another array of objects having the totals by tax_id:

[
 {tax_name: "GST 5%", total_tax: sum of tax_value for tax_id = 1},
 {tax_name: "QST 9.975%", total_tax: sum of tax_value for tax_id = 3},
 {tax_name: "HST 13%", sum of tax_value for tax_id = 2}
]

After that I can loop through this array and display the totals of each tax, adding subtotal and display the total invoice.

Also, I should sort them by tax_select but this it's a thing I can live with.

I have tried to: where selected_taxes is the first array of objects

 for (i = 0; i < selected_taxes.length; i++){
        var sum = 0;
    $.each( selected_taxes[i], function( tax_id, tax_value ) {
        sum += tax_value;
    });
    console.log(sum);
 }

but no luck.

Many thanks for your help or suggestions.

like image 964
Adrian P. Avatar asked Dec 28 '25 10:12

Adrian P.


2 Answers

I think Array.prototype.reduce will be your best bet for this:

var totals = data.reduce(function(c,x){
    if(!c[x.tax_id]) c[x.tax_id] = {
        tax_name: x.tax_name,
        tax_id: x.tax_id, 
        total_tax: 0
    };
    c[x.tax_id].total_tax += Number(x.tax_value);
    return c;
}, {});

This approach generates an object that has, as its properties, the tax ID numbers. If you really want a flat array from that, you can convert that into an array after the fact:

var totalsArray = [];
for(var taxId in totals){
    totalsArray.push(totals[taxId]):
}

Demonstration: http://jsfiddle.net/3jaJC/1/

like image 92
Ethan Brown Avatar answered Dec 30 '25 22:12

Ethan Brown


When you're looping over the objects, see what taxId it is and generate a sum for each different taxId.

var sums = {}, obj, i;
for (i = 0; i < selected_taxes.length; i++){
    obj = selected_taxes[i];
    if (!sums[obj.tax_id]) {
        sums[obj.tax_id] = 0;
    }
    sums[obj.tax_id] += +obj.tax_value;
}
console.log(sums); //{ 1:26.46, 2:34.39, 3: 52.76}

http://jsfiddle.net/4X6Wb/

like image 34
Kevin B Avatar answered Dec 30 '25 22:12

Kevin B