javascript - Sum associative array key values and group it by key -


i have table follows :

<table>    <thead>       <th>product</th>       <th>quantity</th>       <th>area</th>       <th>price</th>       <th>total</th>    <tr>       <td id="name">sweets</td>       <td id="qty">10</td>       <td id="area">250</td>       <td id="price">16.50</td>       <td id="total">160.50</td>    </tr>    <tr>       <td id="name"">dry foods</td>       <td id="qty">5</td>       <td id="area">100</td>       <td id="price">10.25</td>       <td id="total">51.25</td>    </tr>    <tr>       <td id="name">fresh</td>       <td id="qty">20</td>       <td id="area">250</td>       <td id="price">5</td>       <td id="total">100</td>    </tr>    <tr>       <td id="name">meat</td>       <td id="qty">10</td>       <td id="area">250</td>       <td id="price">15</td>       <td id="total">150</td>    </tr>    <tr>       <td id="name">frozen</td>       <td id="qty">20</td>       <td id="area">300</td>       <td id="price">10</td>       <td id="total">200</td>    </tr> </table> 

so, want make array {area:total} grouping array values based on area , sum area values.

like :
area 250 : 410.5
area 100 : 51.25
area 300 : 200

i tried follow got array don't know how can grouping areas ( used setinterval function because employees can remove or change area values)

            setinterval(function() {             var $row = $(this).closest("tr");             var sasdata = [];             $row.each(function(i) {                 var sasvalue = parsefloat($row.find("#area").val());                 var totvalue = parsefloat($row.find("#total").val());                 sasdata.push({sas:sasvalue, tot:totvalue});                 console.log(sasdata);             });              function compressedarray(original) {                 var compressed = [];             };         }, 1500) 

could please show me way how can handle issue?

this jsfiddle should solve problem. i've fixed missing thead, double quote in dry foods td, , changes id's classes:

http://jsfiddle.net/q9nrf/1/

var areas = {}; $("tr").each(function() {     var area = $(this).find("td.area").text();     if (area != "") {         var total = parsefloat($(this).find("td.total").text());         if (!areas.hasownproperty(area)) {             areas[area] = 0;                     }         areas[area] += total;     } }); console.log(areas); 

Comments

Popular posts from this blog

django - How can I change user group without delete record -

java - Need to add SOAP security token -

java - EclipseLink JPA Object is not a known entity type -