原生js中json时间相同的放到一起
2018-03-27 13:37
239 查看
第一种
第二种
var a=[{ "id":1, "type":3, "date":"2018-03-26 23:44:15" },{ "id":2, "type":1, "date":"2018-03-26 23:44:15" },{ "id":3, "type":5, "date":"2018-03-26 23:44:18" }] var b={} a.forEach(v=>{ b[v.date]||(b[v.date]=[]) b[v.date]&&b[v.date].push(v) }) console.log(b) VM147521:19 {2018-03-26 23:44:15: Array(2), 2018-03-26 23:44:18: Array(1)}2018-03-26 23:44:15: (2) [{…}, {…}]0: {id: 1, type: 3, date: "2018-03-26 23:44:15"}1: {id: 2, type: 1, date: "2018-03-26 23:44:15"}length: 2__proto__: Array(0)2018-03-26 23:44:18: [{…}]__proto__: Objectconstructor: ƒ Object()hasOwnProperty: ƒ hasOwnProperty()isPrototypeOf: ƒ isPrototypeOf()propertyIsEnumerable: ƒ propertyIsEnumerable()toLocaleString: ƒ toLocaleString()toString: ƒ toString()valueOf: ƒ valueOf()__defineGetter__: ƒ __defineGetter__()__defineSetter__: ƒ __defineSetter__()__lookupGetter__: ƒ __lookupGetter__()__lookupSetter__: ƒ __lookupSetter__()get __proto__: ƒ __proto__()set __proto__: ƒ __proto__() undefined
第二种
var orders=[{ "id":1, "type":3, "date":"2018-03-26 23:44:15" },{ "id":2, "type":1, "date":"2018-03-26 23:44:15" },{ "id":3, "type":5, "date":"2018-03-26 23:44:18" }] var b={}; var New=new Array(); orders.forEach(v=>{ /*b[v.date]||(b[v.date]=[]) b[v.date]&&b[v.date].push(v)*/ !b[v.date]?(b[v.date]=[v]):b[v.date].push(v); //b[v.date]?b[v.date].push(v):b[v.date]=[v] }) var i=0; for(var o in b){ New[i]={ "time":o, "oarr":b[o] } i++; } console.log(New) VM147520:28 (2) [{…}, {…}]0: oarr: (2) [{…}, {…}]0: {id: 1, type: 3, date: "2018-03-26 23:44:15"}1: {id: 2, type: 1, date: "2018-03-26 23:44:15"}date: "2018-03-26 23:44:15"id: 2type: 1__proto__: Objectlength: 2__proto__: Array(0)time: "2018-03-26 23:44:15"__proto__: Object1: oarr: [{…}]0: {id: 3, type: 5, date: "2018-03-26 23:44:18"}length: 1__proto__: Array(0)time: "2018-03-26 23:44:18"__proto__: Objectlength: 2__proto__: Array(0) undefined
相关文章推荐
- 如何通过js处理相同时间的信息整合到一起的问题
- 如何通过js处理相同时间的信息整合到一起的问题
- js转换json格式时间成字符串前台显示
- Java转换Json日期/Date(1487053489965+0800)/格式以及js时间格式 Tue Feb 14 2017 14:06:32 GMT+0800
- 原生Js实现按数据源均分时间点幻灯效果(已封装)
- 原生js如何解决时间委派
- 原生JS返回服务器端时间
- 使用原生JS的AJAX读取json全过程
- js格式化json时间
- js 处理Json 时间带T 时间格式
- js原生JSON学习笔记
- jquery ajax、 原生js ajax、string和json之间的转换
- 原生JS获取系统当前日期和时间
- json.js与ExtJS一起使用的问题
- 原生js,jquery通过ajax获得后台json数据动态新增页面元素
- bootstrap时间控件随滚动条一起移动bootstrap-datetimepicker.js
- 原生js可爱糖果数字时间特效
- json时间格式转成js时间
- js获取json对象中正确的时间格式
- 原生js调用json方法