zoukankan      html  css  js  c++  java
  • JS去重函数的扩展应用

    数据:

    账单id[1,1,1,1,2,2,2,3,3,3,3,3,3,3],

    相对于账单id的金额[100,120,110,150,200,180,220,115,150,120,180,220,140,200],

    要求:

    去重账单id数组,按id分组账单金额总额数组,按账单分组账单金额明细(二维数组)。

    JS方法实现:(该方法的前提是id与金额一一对应,且id已经排序)

      /**
       * 重构公司id和金额数组
       * @param arrId:companyId数组
       * @param arrAmount:金额数组
       * @returns ids:去重后的id数组
       * @returns amount:按id分组求和的金额数组
       * @returns groupAmount:按id分组的二位金额数组
       */
      function uniqueArray(arrId,arrAmount){
          var ids = new Array(); //存放id新数组
          var amount = new Array();//存放金额新数组
          var groupAmount = new Array();//分组金额数组
          var res =[ids,amount,groupAmount];//结果数组
          var json = {};
          var index =0;
          for(var i = 0; i < arrId.length; i++){
              if(!json[arrId[i]]){//不重复操作
                  ids.push(arrId[i]);
                  amount.push(arrAmount[i]);
                  json[arrId[i]] = 1;
                  groupAmount[index]=new Array();//声明二维数组
                  groupAmount[index].push(arrAmount[i]);
                  index++;
              }else{ //重复时操作
                  amount[index-1] = parseFloat(amount[index-1])+ parseFloat(arrAmount[i]);
                  groupAmount[index-1].push(arrAmount[i]);
              }  
          }  
          return res;
      }
    无序id的实现后续。。。

    无序id数组的实现,示例代码如下:

    <!DOCTYPE html>
    <html>
    <head lang="en">
        <meta charset="UTF-8">
        <title></title>
    </head>
    <script type="text/javascript">
        var arrId =[1,1,2,1,3,1,2,3,3,3,2,3,3,3];
        var arrAmount = [100,120,110,150,200,180,220,115,150,120,180,220,140,200];
        var tmpRes = uniqueArray(arrId,arrAmount);
        alert(tmpRes[0]+'----'+tmpRes[1]+'----'+tmpRes[2][0]+'||'+tmpRes[2][1]+'||'+tmpRes[2][2]);
        function uniqueArray(arrId,arrAmount){
            var ids = new Array(); //存放id新数组
            var amount = new Array();//存放金额新数组
            var groupAmount = new Array();//分组金额数组
            var res =[ids,amount,groupAmount];//结果数组
            var json = {};
            var index =0;
            for(var i = 0; i < arrId.length; i++){
                if(!json[arrId[i]]){//不重复操作
                    ids.push(arrId[i]);
                    amount.push(arrAmount[i]);
                    json[arrId[i]] = 1;
                    groupAmount[index]=new Array();//声明二维数组
                    groupAmount[index].push(arrAmount[i]);
                    index++;
                }else{ //重复时操作
                    //求arrId[i]的值位于ids的位置--这样才保证无序也可以实现
                    var ins = getIndexofOb(ids,arrId[i]);
    //                amount[index-1] = parseFloat(amount[index-1])+ parseFloat(arrAmount[i]);
    //                groupAmount[index-1].push(arrAmount[i]);
                    amount[ins] = parseFloat(amount[ins])+ parseFloat(arrAmount[i]);
                    groupAmount[ins].push(arrAmount[i]);
                }
            }
            return res;
        }
    
        /**
         * 返回ob在arrOb中索引值,若不存在,返回-1
         * @param arrOb
         * @param ob
         * @returns {number}
         */
        function getIndexofOb(arrOb,ob){
            for(var i= 0;i<arrOb.length;i++){
                if(ob==arrOb[i]){
                    return i;
                }
            }
            return -1;
        }
    </script>
    <body>
    
    </body>
    </html>


  • 相关阅读:
    effective C++
    bat取时间间隔
    bat设置windows计划任务
    listener.ora 与 tnsnames.ora
    route(windows)
    bat 数组实现
    非const引用参数传入不同类型编译不过的理解(拒绝将临时对象绑定为非const的引用的形参是有道理的)
    python no module named builtins
    Caffe使用新版本CUDA和CuDNN
    Ubuntu16.04安装vim8
  • 原文地址:https://www.cnblogs.com/archermeng/p/7537275.html
Copyright © 2011-2022 走看看