Javascript-about data statistics methods, jQuery, and urgent online.

Source: Internet
Author: User
A code similar to {code...} is generated on the webpage ...} situation: 1. there are many product names, some of which are the same. there is an event under each item, and the event may have different requirements: I need to make a statistical table, and products with the same product name are integrated and categorized according to the different events... A similar


  
  ... 
   
Product Name Quantity Event
Product Name Quantity Event
Product Name Quantity Event

Situation:
1. There are many product names, some of which are the same
2. Each item has an event, which may be different.

Requirements:
I need to make a statistical table. products with the same product name are integrated and classified based on different events, and then count the number of products for different events after integration.

Example:


  
  
Aircraft 1 Damaged
Tank 2 Repair
Aircraft 2 Repair
Aircraft 3 Damaged

Sample requirement results

There are 6 aircraft, 4 damaged, 2 tanks, 2 repair

Existing data

$('#tbody tr').each(function(index) {});

Reply content:

A similar


  
  ... 
   
Product Name Quantity Event
Product Name Quantity Event
Product Name Quantity Event

Situation:
1. There are many product names, some of which are the same
2. Each item has an event, which may be different.

Requirements:
I need to make a statistical table. products with the same product name are integrated and classified based on different events, and then count the number of products for different events after integration.

Example:


  
  
Aircraft 1 Damaged
Tank 2 Repair
Aircraft 2 Repair
Aircraft 3 Damaged

Sample requirement results

There are 6 aircraft, 4 damaged, 2 tanks, 2 repair

Existing data

$('#tbody tr').each(function(index) {});

// Define the object storing the statistical result // JS object you can understand as a Hash Data Structure // the final data structure is // {// 'product key1 ': {// prod: 'product name 1', // event: {// 'event type key1': {type: 'event type 1', count: event quantity }, // 'event type key2': {type: 'event type 2', count: event quantity} //}, // 'product key2 ': {// prod: 'product name2', // event: {// 'event type key3': {type: 'event type 3', count: event quantity }, // 'event type key4': {type: 'event type 4', count: Number of events }//}//},//... //} var result = {}; var fieldMapping = ['prod', 'Count', 'eventtype']; $ ('tbody tr '). each (function (Index, item) {// defines the object that stores a row of Data var lineData ={}; // searches for all td elements under tr and traverses it $ (item ). find ('td '). each (function (index, item) {// read the text content var value under the td element $ (item ). text (); // fieldMapping [index] reads the defined attribute name by location, and assigns the read content to lineData // 1st td to the product name, and 2nd td to the quantity, 3rd are event-type lineData [fieldMapping [index] = value ;}); // obtain the sorted row data and perform statistical processing on it processRow (lineData );}); // process the row of data obtained, and calculate function processRow (lineData) by product or event type {// determine whether a product attribute exists in the result object, If not, create this property // and assign a statistic object to this property if (! Result [lineData. prod]) {result [lineData. prod] = {prod: lineData. prod, event :{};}// read the specific type of attribute data under the event object var event = result [lineData. prod] ['event'] [lineData. eventType]; // if this event type does not exist, add the event type and set the number to 0 if (! Event) {event = {type: lineData. eventType, count: 0}; result [lineData. prod] ['event'] [lineData. eventType] = event;} // accumulate the number of events in the current row to the event count statistics object. count = parseInt (lineData. count) + event. count;} console. log (result );

Shows the running result:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.