javascript - 关于数据统计方法,jQuery,急在线等。
网页中生成了一个类似
<code><table> <tr> <td>商品名称</td> <td> </td> <td>数量</td> <td> </td> <td>事件</td> <td> </td> </tr> <tr> <td>商品名称</td> <td> </td> <td>数量</td> <td> </td> <td>事件</td> <td> </td> </tr> <tr> <td>商品名称</td> <td> </td> <td>数量</td> <td> </td> <td>事件</td> <td> </td> </tr> ... </table></code>
情况:
1.商品名称有很多种,其中会有一些是相同的
2.每个商品下都有一个事件,事件可能会不相同
需求:
我需要做一个统计表,相同的商品名的商品整合在一起,并且根据不的事件进行分类,再统计整合后的不同事件的商品数量。
例子:
<code><table> <tr> <td>飞机</td> <td> </td> <td>1</td> <td> </td> <td>损坏</td> <td> </td> </tr> <tr> <td>坦克</td> <td> </td> <td>2</td> <td> </td> <td>维修</td> <td> </td> </tr> <tr> <td>飞机</td> <td> </td> <td>2</td> <td> </td> <td>维修</td> <td> </td> </tr> <tr> <td>飞机</td> <td> </td> <td>3</td> <td> </td> <td>损坏</td> <td> </td> </tr> </table></code>
示例需求效果
<code>飞机共6架,损坏4架,维修2架 坦克共2架,维修2架 </code>
现有数据
<code>$('#tbody tr').each(function(index) { });</code>
回复内容:
网页中生成了一个类似
<code><table> <tr> <td>商品名称</td> <td> </td> <td>数量</td> <td> </td> <td>事件</td> <td> </td> </tr> <tr> <td>商品名称</td> <td> </td> <td>数量</td> <td> </td> <td>事件</td> <td> </td> </tr> <tr> <td>商品名称</td> <td> </td> <td>数量</td> <td> </td> <td>事件</td> <td> </td> </tr> ... </table></code>
情况:
1.商品名称有很多种,其中会有一些是相同的
2.每个商品下都有一个事件,事件可能会不相同
需求:
我需要做一个统计表,相同的商品名的商品整合在一起,并且根据不的事件进行分类,再统计整合后的不同事件的商品数量。
例子:
<code><table> <tr> <td>飞机</td> <td> </td> <td>1</td> <td> </td> <td>损坏</td> <td> </td> </tr> <tr> <td>坦克</td> <td> </td> <td>2</td> <td> </td> <td>维修</td> <td> </td> </tr> <tr> <td>飞机</td> <td> </td> <td>2</td> <td> </td> <td>维修</td> <td> </td> </tr> <tr> <td>飞机</td> <td> </td> <td>3</td> <td> </td> <td>损坏</td> <td> </td> </tr> </table></code>
示例需求效果
<code>飞机共6架,损坏4架,维修2架 坦克共2架,维修2架 </code>
现有数据
<code>$('#tbody tr').each(function(index) { });</code>
<code>//定义存放统计结果的对象 //JS的对象你可将其理解成为一个Hash数据结构 //最后的数据结构为 //{ // '产品key1':{ // prod:'产品名1', // event:{ // ‘事件类型Key1’:{type:'事件类型1',count:事件数量}, // ‘事件类型Key2’:{type:'事件类型2',count:事件数量} // } // }, // '产品key2':{ // prod:'产品名2', // event:{ // ‘事件类型Key3’:{type:'事件类型3',count:事件数量}, // ‘事件类型Key4’:{type:'事件类型4',count:事件数量} // } // }, // ... // } var result={}; var fieldMapping=['prod','count','eventType']; $('tbody tr').each(function(index,item) { //定义存放一行数据的对象 var lineData={}; //查找tr下的所有td元素,对其遍历 $(item).find('td').each(function(index,item){ //读取td元素下的文本内容 var value=$(item).text(); //fieldMapping[index] 按位置读取定义的属性名,并将读取的内容赋给lineData //第1个td为产品名,第2个td为数量,第3个为事件类型 lineData[fieldMapping[index]]=value; }); //获取整理行数据后,对其进行统计处理 processRow(lineData); }); //处理获取到的一行数据,按产品,及事件类型统计 function processRow(lineData){ //判断result对象中是否有 某个产品的属性,如果不存在,那么创建这个属性 //并将一个统计对象赋值给这个属性 if(!result[lineData.prod]){ result[lineData.prod]={ prod: lineData.prod, event:{} }; } //读取event对象下的特定类型的属性数据 var event=result[lineData.prod]['event'][lineData.eventType]; //如果这个事件类型不存在,那么添加事件类型,并设置数量为0 if(!event){ event={ type: lineData.eventType, count:0 }; result[lineData.prod]['event'][lineData.eventType]=event; } //将当前行数据中的事件数量信息累加到事件数量统计对象上 event.count=parseInt(lineData.count)+event.count; } console.log(result);</code>
运行的结果如下图所示:

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

React combines JSX and HTML to improve user experience. 1) JSX embeds HTML to make development more intuitive. 2) The virtual DOM mechanism optimizes performance and reduces DOM operations. 3) Component-based management UI to improve maintainability. 4) State management and event processing enhance interactivity.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
