How filter and sort array by category and date using Lodash

Viewed 8088

Hello to all I am new in this, I try sort and filter array of object using lodash, I don't know is the correct solution but I know is work, I have array some like this...

contactList =[    { 
      "ChatCount": 2, "chatid": 10000413, "createdon": "2018-10-25T13:49:50.9900000", "isArchive": 0, "isOnline": false ,
      "members":[{"id": "60259166", "lastseen": 15261867,"name": "la", "picture": "/la/5285871.250.jpg"}],
      "message": "",
      "message_cnt": 1,
      "messageid": 1,
      "newest_message": "2018-10-25T13:49:50.9900000",
      "oldest_message": "2018-10-25T13:49:50.9900000",
      "received_cnt": 0,
      "sentby": [{"id":59,"name":"betsynray","picture":"/betsynray/2884P1010025250.jpg","lastseen":1164}],
      "unread_cnt": 1
     },
     {
         "ChatCount": 2, "chatid": 61247987, "createdon": "2018-10-25T13:49:14.9170000", "isArchive": 0,"isOnline": true,
         "members":[{"id": 61247987, "lastseen": 15318187,"name": "li", "picture": "/li/4705502.250.jpg"}],
         "message": "good",
        "message_cnt": 2,
        "messageid": 2,
        "newest_message": "2018-10-31T10:20:29.5000000",
        "oldest_message": "2018-10-25T13:47:59.6700000",
        "received_cnt": 0,
        "sentby": [{"id":59,"name":"mm","picture":"/mm/2884P1010025250.jpg","lastseen":1164}],
        "unread_cnt": 3
     }

]

them I want to filter (sentBy.lastseen > 18000) and sort by most recent date using (newest_message) I have lodahs and when I try for example sort by date I'm using some like this...

contactList.sort(function (o) { return moment(o.NEWEST_MESSAGE).format('YYYYMMDD')}).reverse();

This working but now I try filter also by lastseen I try something like this using lodas and momentjs

 function any() {

   var status = _(contactList)
        .filter(function (a) { return a.MEMBERS[0].lastseen > 1800000 }) 
        .sort(function (o) { return moment(o.NEWEST_MESSAGE).format('YYYYMMDD')}).reverse() // sort names
        .value();
        return status;

}

the latseen filter is work well but no the sort by recent date, What am I doing wrong someone can help me? thank you very much in advance

3 Answers

can you try just to format it to a timestamp:

.sort(function (o) { return moment(o.newest_message).unix()})

And if I see it right you dont need to wrap contactList in a lodash function...

filter,sort and reverse are native array functions.


var status = contactList
        .filter(function (a) { return a.members[0].lastseen > 1800000 })
        .sort(function (o) { return moment(o.newest_message).unix() })
        .reverse();
return status;

What you need is order by with simple Date object, moment is not at all required (however you can still use for complex date logic.

It should be just:

_.orderBy(contactList, o=>new Date(o.newest_message), 'desc')

Here is an working example:

var contactList =[    { 
      "ChatCount": 2, "chatid": 10000413, "createdon": "2018-10-25T13:49:50.9900000", "isArchive": 0, "isOnline": false ,
      "members":[{"id": "60259166", "lastseen": 15261867,"name": "la", "picture": "/la/5285871.250.jpg"}],
      "message": "",
      "message_cnt": 1,
      "messageid": 1,
      "newest_message": "2018-10-25T13:49:50.9900000",
      "oldest_message": "2018-10-25T13:49:50.9900000",
      "received_cnt": 0,
      "sentby": [{"id":59,"name":"betsynray","picture":"/betsynray/2884P1010025250.jpg","lastseen":1164}],
      "unread_cnt": 1
     },
     {
         "ChatCount": 2, "chatid": 61247987, "createdon": "2018-10-25T13:49:14.9170000", "isArchive": 0,"isOnline": true,
         "members":[{"id": 61247987, "lastseen": 15318187,"name": "li", "picture": "/li/4705502.250.jpg"}],
         "message": "good",
        "message_cnt": 2,
        "messageid": 2,
        "newest_message": "2018-10-31T10:20:29.5000000",
        "oldest_message": "2018-10-25T13:47:59.6700000",
        "received_cnt": 0,
        "sentby": [{"id":59,"name":"mm","picture":"/mm/2884P1010025250.jpg","lastseen":1164}],
        "unread_cnt": 3
     }

];

var st = _(contactList)
        .filter(a => a.members[0].lastseen > 1800000) 
        .orderBy(o=>new Date(o.newest_message), 'desc')
        .value();
        
console.log(st);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

You can use asc for ascending and desc for descending sorting (the last arg). and also you can pass any other field you need for sorting by name (in case with simple value) or you can pass a callback returning the value based on it will sort.

Since your dates are in ISO format you don't really need anything but orderBy with desc like this:

var data = [{ "ChatCount": 2, "chatid": 10000413, "createdon": "2018-10-25T13:49:50.9900000", "isArchive": 0, "isOnline": false, "members": [{ "id": "60259166", "lastseen": 15261867, "name": "la", "picture": "/la/5285871.250.jpg" }], "message": "", "message_cnt": 1, "messageid": 1, "newest_message": "2018-10-25T13:49:50.9900000", "oldest_message": "2018-10-25T13:49:50.9900000", "received_cnt": 0, "sentby": [{ "id": 59, "name": "betsynray", "picture": "/betsynray/2884P1010025250.jpg", "lastseen": 1164 }], "unread_cnt": 1 }, { "ChatCount": 2, "chatid": 61247987, "createdon": "2018-10-25T13:49:14.9170000", "isArchive": 0, "isOnline": true, "members": [{ "id": 61247987, "lastseen": 15318187, "name": "li", "picture": "/li/4705502.250.jpg" }], "message": "good", "message_cnt": 2, "messageid": 2, "newest_message": "2018-10-31T10:20:29.5000000", "oldest_message": "2018-10-25T13:47:59.6700000", "received_cnt": 0, "sentby": [{ "id": 59, "name": "mm", "picture": "/mm/2884P1010025250.jpg", "lastseen": 1164 }], "unread_cnt": 3 } ];

var result = _(data)
  .filter(x => _.get(x, 'members.0.lastseen') > 1800000)
  .orderBy('newest_message', 'desc')
  .value();

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>

Related