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& ...
随机推荐
- 记一次压测问题定位:connection reset by peer,TCP三次握手后服务端发送RST_网络_c359719435的专栏-CSDN博客 https://blog.csdn.net/c359719435/article/details/80300433
记一次压测问题定位:connection reset by peer,TCP三次握手后服务端发送RST_网络_c359719435的专栏-CSDN博客 https://blog.csdn.net/c3 ...
- tee MultiWriter creates a writer that duplicates its writes to all the // provided writers, similar to the Unix tee(1) command.
https://zh.wikipedia.org/wiki/Tee 在计算机科学中,tee是一个常见的指令,它能够将某个指令的标准输出,导向.存入某个档案中.许多不同的命令行界面(Shell)都提供这 ...
- CF486B
扯在前面 本人找规律找了很长时间,然后发现找到规律之后其实是lj题,于是五分钟敲完代码,然后WA了两发 正文 题意: A, B 都是 n*m 的 01 矩阵,已知 B 矩阵是由A矩阵以一种规则生成 B ...
- LOJ10019生日蛋糕
Mr.W 要制作一个体积为 N*π 的 M 层生日蛋糕,每层都是一个圆柱体. 设从下往上数第 i 蛋糕是半径为 R_i,高度为 H_i 的圆柱.当 i<M 时,要求 R_i>R_{i+1} ...
- python 拼接字
在编译脚本的时候,由于脚本的框架是统一写好的,于是乎用上了拼接字的功能, 本脚本实现的是波特率设置的自动化,利用的是正则表达式,TASK函数是统一写好的,此处只做调用 from Args import ...
- 静默安装Oracle也没那么恐怖
几种必须静默安装的情况 服务器为了减少资源占用,没安装图形组件 不能进入机房,只能远程SSH 想炫(Z)耀(B),静默安装显得有技术含量 磁盘分区要求 如没有特别要求,装机时可按如下分区比较好管理 / ...
- 七:SpringBoot-集成Redis数据库,实现缓存管理
SpringBoot-集成Redis数据库,实现缓存管理 1.SpringBoot集成Redis 1.1 核心依赖 1.2 配置文件 1.3 简单测试案例 1.4 自定义序列化配置 1.5 序列化测试 ...
- cassandra权威指南读书笔记--Cassandra架构(1)
结构 集群-->数据中心-->机架-->节点. cassandra尽可能将数据副本存在多个数据中心,然后读取(查询路由到)尽可能在本地数据中心. 为了去中心化和分区容错性,使用gos ...
- 阶段学习总结-坦克大战(2D)案例
这是前几天学习的案例,这两天学习了NGUI和UGUI,被UI搞得很烦躁,但是今天还是将前几天学习的坦克大战总结以下.这个游戏是小时候的经典红白机游戏的复刻,见截图: 一.游戏物体 游戏中包含地图元素( ...
- C++ Socket 入门
Socket 入门 前置知识 :计算机网络基础(TCP/IP四层模型) Socket 原意是"插座",在计算机通信领域被翻译为"套接字",以\(\{IP:Por ...