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& ...
随机推荐
- Main event loop
https://developer.apple.com/library/archive/documentation/General/Conceptual/Devpedia-CocoaApp/MainE ...
- tcpdump 参数详解及使用案例
参数 -A 以ASCII码方式显示每一个数据包(不会显示数据包中链路层头部信息). 在抓取包含网页数据的数据包时, 可方便查看数据(nt: 即Handy for capturing web pages ...
- 洛谷 P3704 SDOI2017 数字表格
题意: 给定两个整数 \(n, m\),求: \[\prod_{i = 1} ^ n \prod_{j = 1} ^ m \operatorname{Fib}_{\gcd\left(n, m\righ ...
- Springboot 项目部署到服务器上
项目部署到服务器上,有两种方式,一种 jar 包,一种 war 包 jar包 部署时,后续的域名配置,SSL证书等在nginx中配置 war包 部署时,后续的域名配置可以在tomcat中配置就好,修改 ...
- Spring Boot 微服务应用集成Prometheus + Grafana 实现监控告警
Spring Boot 微服务应用集成Prometheus + Grafana 实现监控告警 一.添加依赖 1.1 Actuator 的 /prometheus端点 二.Prometheus 配置 部 ...
- 通达OA<=11.5版本SQL注入——日程安排
注入点产生位置
- MariaDB数据库 ----数据库简介,用户管理,数据库创建,数据类型、数据增删改(实例演示)
数据库简介 数据库--即电子文件柜,用户可以对文件中的数据进行增,删,改,查等操作. 数据库分类 关系型数据库 关系型数据库管理系统(Relational Database Management Sy ...
- Dubbo官网实战使用技巧
原文链接:Dubbo官网实战使用技巧 1.启动时检查: 我们检查依赖的服务是否启动,可利用下面三个属性,优先级从左到右逐渐降低. 如果服务不是强依赖,或者说服务之间可能存在死循环依赖,我们应该将 ch ...
- 树与图的DFS与BFS
树的DFS 题目:https://www.acwing.com/problem/content/848/ 代码 #include<bits/stdc++.h> using namespac ...
- AtCoder Beginner Contest 172
比赛链接:https://atcoder.jp/contests/abc172/tasks A - Calc 题意 给出一个正整数 $a$,计算 $a + a^2 + a^3$ .($1 \le a ...