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

https://stackoverflow.com/questions/18634087/how-to-convert-a-string-to-a-unix-timestamp-in-javascript



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的更多相关文章

  1. JS 深度拷贝 Object Array

    JS 深度拷贝 Object Array function cloneObj(o) { var isArray = o instanceof Array; var isObject = o insta ...

  2. [JS高程]引用类型(Object、Array)

    引用类型:Object.Array Object: person.name   =>推荐,除非必须使用变量([])来表示 person["name"] 区别:[]可以通过变量 ...

  3. JS对Array进行自定制排序

    JS对Array进行自定制排序,简单的做一个记录,代码如下所示: //Test function function myFunction(){ var myArr = new Array(); var ...

  4. JS数组array常用方法

    JS数组array常用方法 1.检测数组 1)检测对象是否为数组,使用instanceof 操作符 if(value instanceof Array) { //对数组执行某些操作 } 2)获取对象的 ...

  5. 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( ...

  6. JS中Array数组的三大属性用法

    原文:JS中Array数组的三大属性用法 Array数组主要有3大属性,它们分别是length属性.prototype属性和constructor属性. JS操作Array数组的方法及属性 本文总结了 ...

  7. 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 ...

  8. python 全栈开发,Day124(MongoDB初识,增删改查操作,数据类型,$关键字以及$修改器,"$"的奇妙用法,Array Object 的特殊操作,选取跳过排序,客户端操作)

    一.MongoDB初识 什么是MongoDB MongoDB 是一个基于分布式文件存储的数据库.由 C++ 语言编写.旨在为 WEB 应用提供可扩展的高性能数据存储解决方案. MongoDB 是一个介 ...

  9. js create Array ways All In One

    js create Array ways All In One ES6 const arr = [...document.querySelectorAll(`[data-dom="^div& ...

随机推荐

  1. 游戏中的AOI(Area of Interest)算法

    游戏中的AOI(Area of Interest)算法 游戏的AOI算法应该算作游戏的基础核心了,许多逻辑都是因为AOI进出事件驱动的,许多网络同步数据也是因为AOI进出事件产生的.因此,良好的AOI ...

  2. 为什么Redis集群要使用反向代理?

    为什么要使用反向代理? 如果没有方向代理,一台Redis可能需要跟很多个客户端连接: 看着是不是很慌?看没关系,主要是连接需要消耗线程资源,没有代理的话,Redis要将很大一部分的资源用在与客户端建立 ...

  3. 「笔记」AC 自动机

    目录 写在前面 定义 引入 构造 暴力 字典图优化 匹配 在线 离线 复杂度 完整代码 例题 P3796 [模板]AC 自动机(加强版) P3808 [模板]AC 自动机(简单版) 「JSOI2007 ...

  4. 34.vsftpd服务程序--虚拟用户模式

    1.创建用于进行FTP 认证的用户数据库文件,其中奇数行为账户名,偶数行为密码. [root@localhost ~]# cd /etc/vsftpd/ [root@localhost vsftpd] ...

  5. 12.su 命令与sudo 服务

     1.su 命令:解决切换用户身份的需求,使得当前用户在不退出登录的情况下,顺畅地切换到其他用户. 比如从root 管理员切换至普通用户: [root@Centos test]# id uid=0(r ...

  6. Vue-Cli程序环境搭建

    环境搭建 ##1.下载node.js cmd输入 node -v 查看是否能够正确打印出版本号 cmd输入 npm -v 查看是否能够正确打印出版本号 ##2.安装node.js淘宝镜像加速器 ### ...

  7. Commons Collections1分析

    0x01.基础知识铺垫 接下来这个过程将涉及到几个接口和类 1.LazyMap 我们通过下⾯这⾏代码对innerMap进⾏修饰,传出的outerMap即是修饰后的Map: Map outerMap = ...

  8. 洛谷P3796

    题目链接  题意:有n个由小写字母组成的模式串以及一个文本串T.每个模式串可能会在文本串中出现多次.哪些模式串在文本串T中出现的次数最多. 题解:ac自动机模板加强版,开一个数组单独记录各个字符串出现 ...

  9. Codeforces Round #697 (Div. 3) F. Unusual Matrix (思维,数学)

    题意:给你一个矩阵\(a\)和\(b\),你可以对\(a\)的任意一行或任意一列的所有元素xor\(1\)任意次,问最终是否能够得到\(b\). 题解:由\(a\ xor\ b=c\),可得:\(a\ ...

  10. Python实现AES的CBC模式加密和解密过程详解 和 chr() 函数 和 s[a:b:c] 和函数lambda

    1.chr()函数 chr() 用一个范围在 range(256)内的(就是0-255)整数作参数,返回一个对应的字符. 2.s[a:b:c] s=(1,2,3,4,5) 1>. s[a]下标访 ...