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& ...
随机推荐
- java.lang.IllegalStateException Unable to find a @SpringBootConfiguration错误解决方案
问题描述:java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @Co ...
- VS Code 使用教程详解
一.写在前面 1.为什么选择 \(VS\) \(code\) 一款非常好用的代码编辑器 标准化 \(Language\) \(Service\) \(Protocol\) 内置调试器和标准化 \(De ...
- SpringMVC听课笔记(十一:国际化)
1. 关于国际化 -- 在页面上根据浏览器的语言设置情况对文本(不是内容),时间,数值进行本地化处理 使用JSTL的fmt标签 -- 可以在bean中获取国际化资源文件 Locale对应的消息 在be ...
- 配合 jekins—springboot脚本
#!/usr/bin/bash # author : renguangyin@yingu.com current=$(cd `dirname $0`; pwd) cd ${current} ext_n ...
- python——模块、标准库、第三方模块安装
模块(module)简介 模块化--指将一个完整的程序分解为一个一个小的模块,通过将模块组合,来搭建出一个完整的程序. 模块化的特点: ① 方便开发 ② 方便维护 ③ 模块可以复用! 在Python中 ...
- ubuntu14.04 ssh允许root用户远程登录
vi /etc/ssh/sshd_config #注释掉 #PermitRootLogin without-password # Authentication: LoginGraceTime 120 ...
- Shiro权限项目
目录 环境配置 spring容器 springmvc freemarker mybatis shiro 工具类 TokenManager.java Result.java 功能实现 登录 注册 个人中 ...
- 微信小程序分享之生成海报--canvas
首先看文档 了解知识点~~(https://developers.weixin.qq.com/miniprogram/dev/component/) githup:https://github.com ...
- Uva 10815 Andy's First Dictionary(字符串)
题目链接:https://vjudge.net/problem/UVA-10815 题意 找出一段文本中的所有单词,以小写形式按照字典序输出. 思路 用空白符替换文本中所有非字母字符后再次读入. 代码 ...
- bfs输出路径 && 最短路(迪杰斯特拉)输出路径
问题描述 解决方法 1.像第一个问题那就是最短路问题(我代码采用迪杰斯特拉算法)实现 2.换乘次数最少,那就用bfs广搜来寻找答案.但是我的代码不能保证这个最少换乘是最短路程 代码 1 #includ ...