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.
See Question&Answers more detail:
os