如何使用JavaScript基于多个条件汇总行?

时间:2015-03-22 06:52:27

标签: javascript sum summary

我有下表:

+-----------+-------------+------------+--------------+--------+-----+
| Tariff No | Description | Net Weight | Gross Weight | Amount | COO |
+-----------+-------------+------------+--------------+--------+-----+
|    900011 |         xyz |         40 |           50 |   1000 |  IT |
+-----------+-------------+------------+--------------+--------+-----+
|    900011 |         xyz |         40 |           50 |   1500 |  IT |
+-----------+-------------+------------+--------------+--------+-----+
|    400222 |         abc |         50 |           60 |   1000 |  CA |
+-----------+-------------+------------+--------------+--------+-----+
|    400222 |         def |         40 |           50 |   2000 |  DE |
+-----------+-------------+------------+--------------+--------+-----+

我需要总结一下这个表“关税号”和“COO”,以便得到的表格如下:

+-----------+-----+------------+--------------+--------+
| Tariff No | COO | Net Weight | Gross Weight | Amount |
+-----------+-----+------------+--------------+--------+
|    900011 |  IT |         80 |          100 |   2500 |
+-----------+-----+------------+--------------+--------+
|    400222 |  CA |         50 |           60 |   1000 |
+-----------+-----+------------+--------------+--------+
|    400222 |  DE |         40 |           50 |   2000 |
+-----------+-----+------------+--------------+--------+

请注意,生成的表格没有“描述”列。

如何使用JavaScript执行此操作?

1 个答案:

答案 0 :(得分:1)

这就是我使用JavaScript的方式。

  1. 首先,我们需要将表格建模为JavaScript中的数据:

    var table = [
        row(900011, "xyz", 40, 50, 1000, "IT"),
        row(900011, "xyz", 40, 50, 1500, "IT"),
        row(400222, "abc", 50, 60, 1000, "CA"),
        row(400222, "def", 40, 50, 2000, "DE")
    ];
    
    function row( tariffNo
                , description
                , netWeight
                , grossWeight
                , amount
                , coo) {
    
        return { tariffNo    : tariffNo
               , description : description
               , netWeight   : netWeight
               , grossWeight : grossWeight
               , amount      : amount
               , coo         : coo };
    }
    
  2. 接下来,我们创建一个groupBy函数,根据给定的table函数将给定groupOf的行分类为组:

    function groupBy(groupOf, table) {
        var groups = {};
        var values = [];
    
        table.forEach(function (row) {
            var value = groupOf(row);
    
            if (groups.hasOwnProperty(value)) {
                var rows = groups[value];
            } else {
                var rows = groups[value] = [];
                values.push(value);
            }
    
            rows.push(row);
        });
    
        return values.map(getGroup);
    
        function getGroup(value) {
            return groups[value];
        }
    }
    
  3. 然后,我们按照“关税号”和“首席运营官”对输入表进行分组,如下所示:

    var groups = groupBy(groupOf, table);
    
    function groupOf(row) {
        return row.tariffNo + " " + row.coo;
    }
    
  4. 最后,我们使用以下map / reduce操作汇总结果:

    var result = groups.map(summarize);
    
    function summarize(rows) {
        return rows.reduce(summary, { netWeight   : 0
                                    , grossWeight : 0
                                    , amount      : 0 });
    }
    
    function summary(sum, row) {
        return { tariffNo    : row.tariffNo
               , coo         : row.coo
               , netWeight   : sum.netWeight   + row.netWeight
               , grossWeight : sum.grossWeight + row.grossWeight
               , amount      : sum.amount      + row.amount };
    }
    
  5. 这就是全部。把它们放在一起:

    var table = [
        row(900011, "xyz", 40, 50, 1000, "IT"),
        row(900011, "xyz", 40, 50, 1500, "IT"),
        row(400222, "abc", 50, 60, 1000, "CA"),
        row(400222, "def", 40, 50, 2000, "DE")
    ];
    
    var groups = groupBy(groupOf, table);
    
    var result = groups.map(summarize);
    
    alert(JSON.stringify(result, null, 4));
    
    function row( tariffNo
                , description
                , netWeight
                , grossWeight
                , amount
                , coo) {
    
        return { tariffNo    : tariffNo
               , description : description
               , netWeight   : netWeight
               , grossWeight : grossWeight
               , amount      : amount
               , coo         : coo };
    }
    
    function groupBy(groupOf, table) {
        var groups = {};
        var values = [];
    
        table.forEach(function (row) {
            var value = groupOf(row);
    
            if (groups.hasOwnProperty(value)) {
                var rows = groups[value];
            } else {
                var rows = groups[value] = [];
                values.push(value);
            }
    
            rows.push(row);
        });
    
        return values.map(getGroup);
    
        function getGroup(value) {
            return groups[value];
        }
    }
    
    function groupOf(row) {
        return row.tariffNo + " " + row.coo;
    }
    
    function summarize(rows) {
        return rows.reduce(summary, { netWeight   : 0
                                    , grossWeight : 0
                                    , amount      : 0 });
    }
    
    function summary(sum, row) {
        return { tariffNo    : row.tariffNo
               , coo         : row.coo
               , netWeight   : sum.netWeight   + row.netWeight
               , grossWeight : sum.grossWeight + row.grossWeight
               , amount      : sum.amount      + row.amount };
    }

    希望有所帮助。