js 按字段分组
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"}];
var map = {}, //临时记录根据某字段分组的临时键值对变量
dest = []; //最终输出数组
for(var i = 0; i < data.length; i++){ //遍历list数据
var ai = data[i]; //ai代表list中的某一项
if(!map[ai.uname]){ //临时键值对变量中不存在此键
dest.push({ //将此数据以json对象格式放到数组中{key1:value1,key2:value2},key1是分组的字段,key2是list中的某一条数据
uname: ai.uname,
data: [ai]
});
map[ai.uname] = ai; //将未加入map的key放入
}else{ //临时键值对中有此键
for(var j = 0; j < dest.length; j++){ //遍历list
var dj = dest[j]; //结果数组中某一项数据
if(dj.uname == ai.uname){ //结果数据中此dest[key1]的值和该list[i].uname的值相等时
dj.data.push(ai); //把list[i]也放入该dest[key1].data中去
break;
}
}
}
}
console.info(dest);
js 按字段分组的更多相关文章
- js将json格式的list转换为按某个字段分组的map数组
这几天做的微信公众号项目中,出现了需要将list分组显示的需求,解决方法如下 var data = [{ "id": "32b80b76-a81e-4545-8065-1 ...
- 每日学习心得:CustomValidator验证控件验证用户输入的字符长度、Linq 多字段分组统计、ASP.NET后台弹出confirm对话框,然后点击确定,执行一段代码
2013-9-15 1. CustomValidator验证控件验证用户输入的字符长度 在实际的开发中通常会遇到验证用户输入的字符长度的问题,通常的情况下,可以写一个js的脚本或者函数,在ASP ...
- js对数组分组处理
一.js数组分组 1.js对数据分组类似group by 源码如下: <!DOCTYPE html> <html lang="en"> <head&g ...
- SQL Server 按某一字段分组 取 最大 (小)值所在行的数据
SQL Server 按某一字段分组 取 最大 (小)值所在行的数据 -- 按某一字段分组 取 最大 (小)值所在行的数据 -- (爱新觉罗.毓华(十八年风雨,守得冰山雪莲花开) 2007-10-23 ...
- .Net Linq与Lambda表达式中GroupBy以多个字段分组
一.引入 基本上熟悉C#语言的没有不知道Lambda表达式的,其对于数据的处理真的是太方便了.其中分组处理的GroupBy方法在List中的使用非常广泛.正式近期一个功能需求中又遇到了,而且是需要Gr ...
- js判断字段是否为空 isNull
js判断字段是否为空 isNull //在js中if条件为null/undefined/0/NaN/""表达式时,统统被解释为false,此外均为true .//为空判断函数f ...
- JS正则表达式获取分组内容实例
JS正则表达式获取分组内容. 支持多次匹配的方式: var testStr = "now test001 test002"; var re = /test(\d+)/ig; var ...
- mysql按某一字段分组取最大(小)值所在行的数据
mysql按某一字段分组取最大(小)值所在行的数据 mysql技巧--按某一字段分组取最大(小)值所在行的数据,这是mysql数据库程序员经常用到的在处理一些报表数据时候可以活用!那么猎微网将总结 ...
- mysql根据某个字段分组根据更新时间获取最新的记录
我现在有一种统计表,要根据一个字段分组然后根据更新时间,每个分组获取最新的一条记录.命名感觉挺简单的一个需求,然而没什么思路,当然是问度娘了. 度娘的答案很统一,然而都不管用,都是报错的,不知道是不是 ...
随机推荐
- 理解if __name__ == '__main__':
一开始没怎么注意这个语句,这两天在模拟知乎登陆时准备刨根问底了,先看两行代码片段 第一个例子:这是在login.py文件的一部分,其他我们忽略,只关注print()函数的内容 import time ...
- iframe.contentWindow介绍
一.在使用iframe的页面,要操作这个iframe里面的DOM元素可以用: contentWindow.contentDocument(测试的时候chrome浏览器,要在服务器环境下) 1.先获取i ...
- 使用quartz实现定时器功能
首先导入两个包 <dependency> <groupId>org.quartz-scheduler</groupId> <artifactId>qua ...
- Spring MVC文件上传教程 commons-io/commons-uploadfile
Spring MVC文件上传教程 commons-io/commons-uploadfile 用到的依赖jar包: commons-fileupload 1.3.1 commons-io 2.4 基于 ...
- ES5和ES6中的继承
看到一篇写的非常好的关于js继承的文章,其中对构造函数.原型.实例之间的关系的描述十分透彻,故转载作者文章以随时学习,并供大家共同进步! ES5 ES5中的继承,看图: function Super( ...
- 剑指 offer面试题20 顺时针打印矩阵
[题目描述] 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 则依次打印出数字1, ...
- Java网络编程和NIO详解5:Java 非阻塞 IO 和异步 IO
Java网络编程和NIO详解5:Java 非阻塞 IO 和异步 IO Java 非阻塞 IO 和异步 IO 转自https://www.javadoop.com/post/nio-and-aio 本系 ...
- 002——数组(二)each() list() implode() explode() in_array()
<?php /** * 数组(二)each() list() implode() explode() in_array() */ /*implode() 把数组拆分成字符串 * explode( ...
- POJ 1321 棋盘问题 dfs 难度:0
http://poj.org/problem?id=1321 注意是在'#'的地方放棋子 矩阵大小不过8*8,即使是8!的时间复杂度也足以承受,可以直接dfs求解 dfs时标注当前点的行和列已被访问, ...
- 认识Applet和Ajax
一.Applet 1.Applet的定义:Applet是采用Java编程语言编写的小应用程序,该程序可以包含在HTML(标准通用标记语言的一个应用)页中,与在页中包含图像的方式大致相同. Java写出 ...