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
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With