xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

js & sort array object All In One

js & sort array object All In One

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

???




demos

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));



(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!

refs



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2019-08-02 18:03  xgqfrms  阅读(68)  评论(14编辑  收藏  举报