search
HomeBackend DevelopmentPHP Tutorialjavascript - 关于数据统计方法,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>

运行的结果如下图所示:

javascript - 关于数据统计方法,jQuery,急在线等。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Email: Step-by-Step Sending GuidePHP Email: Step-by-Step Sending GuideMay 09, 2025 am 12:14 AM

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

How to Send Email via PHP: Examples & CodeHow to Send Email via PHP: Examples & CodeMay 09, 2025 am 12:13 AM

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

Advanced PHP Email: Custom Headers & FeaturesAdvanced PHP Email: Custom Headers & FeaturesMay 09, 2025 am 12:13 AM

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Guide to Sending Emails with PHP & SMTPGuide to Sending Emails with PHP & SMTPMay 09, 2025 am 12:06 AM

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

PHP Email Security: Best Practices for Sending EmailsPHP Email Security: Best Practices for Sending EmailsMay 08, 2025 am 12:16 AM

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.