Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
687 views
in Technique[技术] by (71.8m points)

javascript - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...