js & sort array object
js & sort array object
sort array object in js
https://flaviocopes.com/how-to-sort-array-of-objects-by-property-javascript/
let msgs = [
{
"senderUid": "6845484",
"receiverUid": "6845481",
"serialNum": "A 1564737163253",
"msgId": 606896983568064500,
"text": "xxxxx",
"time": "17:11",
"count": 1,
"isSelf": true
},
{
"senderUid": "6845484",
"receiverUid": "6845481",
"serialNum": "A 1564737283216",
"msgId": 606897486704189400,
"text": "zzzz",
"time": "17:13",
"count": 2,
"isSelf": true
},
{
"senderUid": "6845481",
"receiverUid": "6845484",
"serialNum": "C 1564735983212",
"msgId": 606892034444533800,
"text": "C 文本消息 text 3",
"time": "16:52",
"count": 3
},
{
"senderUid": "6845481",
"receiverUid": "6845484",
"serialNum": "C 1564735426174",
"msgId": 606889698041045000,
"text": "C 文本消息 text 2",
"time": "16:42",
"count": 3
},
{
"senderUid": "6845481",
"receiverUid": "6845484",
"serialNum": "C 1564730001766",
"msgId": 606866947376980000,
"text": "C 文本消息 text 1",
"time": "15:12",
"count": 3
},
];
let log = console.log;
// time format & msgId
log(`msgs =`, JSON.stringify(msgs, null, 4));
// 1. sort by id
msgs.sort((a, b) => (a.msgId > b.msgId) ? 1 : -1);
log(`msgs =`, JSON.stringify(msgs, null, 4));
// 2. group time
???
demo
https://repl.it/@xgqfrms/date-format-and-5-minutes
https://date-format-and-5-minutes.xgqfrms.repl.run/
let msgs = [
{
"senderUid": "6845481",
"receiverUid": "6845484",
"serialNum": "C 1564735983212",
"msgId": 606892034444533800,
"text": "C 文本消息 text 3",
"time": "16:52",
"count": 3
},
{
"senderUid": "6845481",
"receiverUid": "6845484",
"serialNum": "C 1564735426174",
"msgId": 606889698041045000,
"text": "C 文本消息 text 2",
"time": "16:42",
"count": 3
},
{
"senderUid": "6845481",
"receiverUid": "6845484",
"serialNum": "C 1564730001766",
"msgId": 606866947376980000,
"text": "C 文本消息 text 1",
"time": "15:12",
"count": 3
},
{
"senderUid": "6845484",
"receiverUid": "6845481",
"serialNum": "A 1564737163253",
"msgId": 606896983568064500,
"text": "xxxxx",
"time": "17:11",
"count": 1,
"isSelf": true
},
{
"senderUid": "6845484",
"receiverUid": "6845481",
"serialNum": "A 1564737283216",
"msgId": 606897486704189400,
"text": "zzzz",
"time": "17:13",
"count": 2,
"isSelf": true
}
];
let log = console.log;
// time format & msgId
// log(`msgs =`, JSON.stringify(msgs, null, 4));
// 1. sort by id
// 2. group time
const list = [
{
color: 'white',
size: 'XXL',
num: 3,
},
{
color: 'red',
size: 'XL',
num: 1,
},
{
color: 'black',
size: 'M',
num: 7,
}
];
log(`msgs =`, JSON.stringify(list, null, 4));
list.sort((a, b) => (a.num > b.num) ? 1 : -1);
log(`new msgs =`, JSON.stringify(list, null, 4));
js date string to timestamp
https://www.toptal.com/software/definitive-guide-to-datetime-manipulation
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/parse
bug
OK
// timestamp
let t1 = new Date().getTime();
let t11 = Date.parse('2019/08/02 18:54');
let t2 = new Date("16:52");
log(`t1 =`, t1);
log(`t11 =`, t11);
log(`t2 =`, t2);
seconds
autoGetToday
const autoGetToday = (time = ``, debug = false) => {
let log = console.log;
let date = new Date();
let year = date.getFullYear();
let month = date.getMonth() + 1;
let day = date.getDate();
if (debug) {
log(year);
log(month);
log(day);
}
let today = `${year}/${month}/${day} ${time}`;
if (debug) {
log(`today =`, today);
}
return today;
};
autoGetToday(`19:20`, true);
five minutes
let nowTime = Date.now();
console.log(`nowTime =`, nowTime);
let nowTime1 = Date.parse(`2019/08/02 17:18`);
console.log(`nowTime1 =`, nowTime1);
let nowTime2 = Date.parse(`2019/08/02 17:13`);
console.log(`nowTime2 =`, nowTime2);
let five = nowTime1 - nowTime2;
console.log(`five =`, five);
// mm, ss, ms
const fiveMinutes = 5 * 60 * 1000;
OK
let msgs = [
{
"senderUid": "6845484",
"receiverUid": "6845481",
"serialNum": "A 1564737163253",
"msgId": 606896983568064500,
"text": "B",
"time": "2019/08/02 17:11",
"count": 1,
"isSelf": true
},
{
"senderUid": "6845484",
"receiverUid": "6845481",
"serialNum": "A 1564737283216",
"msgId": 606897486704189400,
"text": "A",
"time": "2019/08/02 17:13",
"count": 2,
"isSelf": true
},
{
"senderUid": "6845481",
"receiverUid": "6845484",
"serialNum": "C 1564735983212",
"msgId": 606892034444533800,
"text": "C",
"time": "2019/08/02 16:52",
"count": 3
},
{
"senderUid": "6845481",
"receiverUid": "6845484",
"serialNum": "C 1564735426174",
"msgId": 606889698041045000,
"text": "D",
"time": "2019/08/02 16:42",
"count": 3
},
{
"senderUid": "6845481",
"receiverUid": "6845484",
"serialNum": "C 1564730001766",
"msgId": 606866947376980000,
"text": "E",
"time": "2019/08/02 15:12",
"count": 3
},
{
"senderUid": "6845481",
"receiverUid": "6845484",
"serialNum": "C 1564730001766",
"msgId": 606866947376970000,
"text": "G",
"time": "2019/08/01 5:12",
"count": 3
},
{
"senderUid": "6845481",
"receiverUid": "6845484",
"serialNum": "C 1564730001766",
"msgId": 606866947376910000,
"text": "F",
"time": "2019/08/01 15:12",
"count": 3
},
];
let log = console.log;
// time format & msgId
// log(`msgs =`, JSON.stringify(msgs, null, 4));
// 1. sort by id
// asc & ascending
// msgs.sort((a, b) => (a.msgId > b.msgId) ? 1 : -1);
// desc & descending
msgs.sort((a, b) => (a.msgId < b.msgId) ? 1 : -1);
// log(`msgs =`, JSON.stringify(msgs, null, 4));
// 2. group time
// TODO & groups
let nowTime = Date.now();
// log(`nowTime =`, nowTime);
// mm, ss, ms
const fiveMinutes = 5 * 60 * 1000;
let DB = {};
let len = msgs.length;
let groupFlag = ``;
if (msgs[0]) {
groupFlag = Date.parse(msgs[0].time);
}
let cp_msgs = msgs;
let finished = false;
// group flag
for (let i = 0; i < len; i++) {
// filter
let new_cp_msgs = cp_msgs.filter(
(obj, k) => {
let {
time,
} = obj;
if (time.includes(`/`)) {
time = Date.parse(time);
} else {
let today = autoGetToday(time);
time = Date.parse(today);
}
let min = Math.abs(groupFlag - time);
if (min <= fiveMinutes) {
// DB[i].push(obj);
} else {
return obj;
}
}
);
if (cp_msgs.length <= 1 && finished) {
break;
} else {
DB[i] = [];
}
// if (cp_msgs.length <= 1) {
// break;
// }
log(`cp_msgs =`, cp_msgs);
for (let j = 0; j < cp_msgs.length; j++) {
if (cp_msgs.length === 1) {
finished = true;
}
let obj = cp_msgs[j];
let {
time,
} = obj;
if (time.includes(`/`)) {
time = Date.parse(time);
} else {
let today = autoGetToday(time);
time = Date.parse(today);
}
let min = Math.abs(groupFlag - time);
if (min <= fiveMinutes) {
DB[i].push(obj);
} else {
groupFlag = time;
cp_msgs = new_cp_msgs;
break;
}
}
}
const autoGetToday = (time = ``, debug = false) => {
let log = console.log;
let date = new Date();
let year = date.getFullYear();
let month = date.getMonth() + 1;
let day = date.getDate();
if (debug) {
log(year);
log(month);
log(day);
}
let today = `${year}/${month}/${day} ${time}`;
if (debug) {
log(`today =`, today);
}
return today;
};
console.log(`DB groups =`, JSON.stringify(DB, null, 4));
xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
js & sort array object的更多相关文章
- JS 深度拷贝 Object Array
JS 深度拷贝 Object Array function cloneObj(o) { var isArray = o instanceof Array; var isObject = o insta ...
- [JS高程]引用类型(Object、Array)
引用类型:Object.Array Object: person.name =>推荐,除非必须使用变量([])来表示 person["name"] 区别:[]可以通过变量 ...
- JS对Array进行自定制排序
JS对Array进行自定制排序,简单的做一个记录,代码如下所示: //Test function function myFunction(){ var myArr = new Array(); var ...
- JS数组array常用方法
JS数组array常用方法 1.检测数组 1)检测对象是否为数组,使用instanceof 操作符 if(value instanceof Array) { //对数组执行某些操作 } 2)获取对象的 ...
- js sort map by key
js sort map by key Map map to array // Array.from() Object let obj = {}; for(let key of Object.keys( ...
- JS中Array数组的三大属性用法
原文:JS中Array数组的三大属性用法 Array数组主要有3大属性,它们分别是length属性.prototype属性和constructor属性. JS操作Array数组的方法及属性 本文总结了 ...
- LeetCode 905. Sort Array By Parity
905. Sort Array By Parity Given an array A of non-negative integers, return an array consisting of a ...
- python 全栈开发,Day124(MongoDB初识,增删改查操作,数据类型,$关键字以及$修改器,"$"的奇妙用法,Array Object 的特殊操作,选取跳过排序,客户端操作)
一.MongoDB初识 什么是MongoDB MongoDB 是一个基于分布式文件存储的数据库.由 C++ 语言编写.旨在为 WEB 应用提供可扩展的高性能数据存储解决方案. MongoDB 是一个介 ...
- js create Array ways All In One
js create Array ways All In One ES6 const arr = [...document.querySelectorAll(`[data-dom="^div& ...
随机推荐
- 慕课网金职位 Python工程师 百度网盘下载
百度网盘链接:https://pan.baidu.com/s/1xshLRO3ru0LAsQQ0pE67Qg 提取码:bh9f 如果失效加我微信:610060008[视频不加密,资料代码齐全,超清一手 ...
- 关于js中each()使用return不能终止循环
Jquery的each里面用return false代替break:return ture代替continue $(xx).each(function() { if(xx){ return false ...
- redis性能优化、内存分析及优化
redis性能优化.内存分析及优化 1.优化网络延时 2.警惕执行时间长的操作 3.优化数据结构.使用正确的算法 4.考虑操作系统和硬件是否影响性能 5.考虑持久化带来的开销 5.1 RDB 全量持久 ...
- JavaWeb——EL及JSTL学习总结
什么是EL表达式 为什么需要EL EL的主要作用 EL的语法 EL的开发步骤 EL实例练习 EL中的运算符 EL表达式显示内容的特点 EL的特点 EL隐式对象 EL隐式对象介绍 隐式对象实例练习 什么 ...
- 希尔伯特曲线python3实现
需要OpenGL库:https://www.lfd.uci.edu/~gohlke/pythonlibs/#pyopengl #coding:utf-8 from OpenGL.GL import * ...
- CVE-2018-4407(IOS缓冲区溢出漏洞)exp
CVE-2018-4407为ios缓冲区溢出漏洞 exp: import scapyfrom scapy.all import * send(IP(dst="同一局域网内目标Ip" ...
- 从零搭建一个IdentityServer——聊聊Asp.net core中的身份验证与授权
OpenIDConnect是一个身份验证服务,而Oauth2.0是一个授权框架,在前面几篇文章里通过IdentityServer4实现了基于Oauth2.0的客户端证书(Client_Credenti ...
- JVM之堆参数
1.Java 7和Java 8区别 Java 7堆结构 JDK 1.8之后将最初的永久代取消了,由元空间取代. 在Java8中,永久代已经被移除,被一个称为元空间的区域所取代.元空间的本质和永久代类似 ...
- UDP实现多人聊天
发送端 package com.zy.exercise; import java.net.DatagramPacket; import java.net.DatagramSocket; import ...
- Educational Codeforces Round 86 (Div. 2)
比赛链接:https://codeforces.com/contest/1342 A - Road To Zero 题意 有两个非负整数 x, y 以及两种操作: 支付 a 点代价使其中一个数加一或减 ...