• js 按字段分组


     1 var data = [{"id":"32b80b76-a81e-4545-8065-1e7c57180801","userId":"qy-uid","qcCode":"140403123001","packageInfo":"秋雨包裹一","tjTime":"2017-12-21 10:38:31.0","pdTime":"2017-12-21 11:05:08.0","shTime":"2017-12-29 10:17:06.0","addUserId":"qy-uid","pdUserId":"qy-uid","shUserId":"qy-uid","pdState":"00","shState":"00","packageState":"00","deleteFlg":"00","uname":"秋雨","uphone":"18151449650"},{"id":"9ee2e7f0-0f7a-44e2-b7cc-577d21eb1142","userId":"recorde-test-for-qy-do-not-delete","qcCode":"queen-qcCode003","packageInfo":"绿箭侠包裹三","tjTime":"2017-12-21 10:46:08.0","tijiaoTime":"2017-12-21 10:46:27.0","shTime":"2017-12-29 10:16:59.0","addUserId":"recorde-test-for-qy-do-not-delete","tjUserId":"recorde-test-for-qy-do-not-delete","shUserId":"qy-uid","tjState":"01","shState":"00","packageState":"00","deleteFlg":"00","uname":"奥利弗 ","uphone":"1666666666"},{"id":"a35b31b6-b974-4ca6-a4b6-28db3dae9814","userId":"recorde-test-for-qy-do-not-delete","qcCode":"queen-qcCode002","packageInfo":"绿箭侠包裹二","tjTime":"2017-12-21 10:45:44.0","tijiaoTime":"2017-12-21 10:46:27.0","pdTime":"2017-12-21 11:05:26.0","shTime":"2017-12-29 10:17:02.0","addUserId":"recorde-test-for-qy-do-not-delete","tjUserId":"recorde-test-for-qy-do-not-delete","pdUserId":"qy-uid","shUserId":"qy-uid","tjState":"01","pdState":"00","shState":"00","packageState":"00","deleteFlg":"00","uname":"奥利弗 ","uphone":"1666666666"},{"id":"d265b81f-4459-4986-bce7-8ef6f4ff3751","userId":"qy-uid","qcCode":"140403123002","packageInfo":"秋雨包裹二","tjTime":"2017-12-21 10:38:47.0","pdTime":"2017-12-21 11:05:13.0","addUserId":"qy-uid","pdUserId":"qy-uid","pdState":"00","packageState":"00","deleteFlg":"00","uname":"秋雨","uphone":"18151449650"},{"id":"f32fc473-4e2c-4357-b0bb-6fb44eb9eb74","userId":"recorde-test-for-qy-do-not-delete","qcCode":"queen-qcCode001","packageInfo":"绿箭侠包裹一","tjTime":"2017-12-21 10:45:27.0","tijiaoTime":"2017-12-21 10:46:27.0","pdTime":"2017-12-21 11:05:20.0","addUserId":"recorde-test-for-qy-do-not-delete","tjUserId":"recorde-test-for-qy-do-not-delete","pdUserId":"qy-uid","tjState":"01","pdState":"00","packageState":"00","deleteFlg":"00","uname":"奥利弗 ","uphone":"1666666666"},{"id":"f830543a-ae54-49ee-9646-b29e81c50d25","userId":"qy-uid","qcCode":"140403123003","packageInfo":"秋雨包裹三","tjTime":"2017-12-21 10:39:00.0","addUserId":"qy-uid","packageState":"00","deleteFlg":"00","uname":"秋雨","uphone":"18151449650"}];
     2                     var map = {},    //临时记录根据某字段分组的临时键值对变量
     3                         dest = [];   //最终输出数组
     4                     for(var i = 0; i < data.length; i++){    //遍历list数据
     5                         var ai = data[i];    //ai代表list中的某一项
     6                         if(!map[ai.uname]){  //临时键值对变量中不存在此键
     7                             dest.push({     //将此数据以json对象格式放到数组中{key1:value1,key2:value2},key1是分组的字段,key2是list中的某一条数据
     8                                 uname: ai.uname,
     9                                 data: [ai]
    10                             });
    11                             map[ai.uname] = ai;    //将未加入map的key放入
    12                         }else{   //临时键值对中有此键
    13                             for(var j = 0; j < dest.length; j++){    //遍历list
    14                                 var dj = dest[j];    //结果数组中某一项数据
    15                                 if(dj.uname == ai.uname){    //结果数据中此dest[key1]的值和该list[i].uname的值相等时
    16                                     dj.data.push(ai);    //把list[i]也放入该dest[key1].data中去
    17                                     break;
    18                                 }
    19                             }
    20                         }
    21                     }
    22                     console.info(dest);
  • 相关阅读:
    黑马乐优商城项目总结
    SpringDataJpa学习(3)——SpringDataJpa的多表映射和动态查询
    SpringDataJpa学习(2)——SpringDataJpa的单表使用
    SpringDataJpa学习(1)——Jpa学习
    个人博客06
    个人博客05
    个人博客04
    典型用户和用户场景描述
    个人博客03
    个人博客02
  • 原文地址:https://www.cnblogs.com/starksoft/p/9480070.html
Copyright © 2020-2023  润新知