Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
475 views
in Technique[技术] by (71.8m points)

javascript - how to show data date wise according to json response?

i have a json response in this format:

[{"message":"fsbdfs","id":"8290","readBy":"2016-05-25 06:17:01","userID":"339","dateTime":"2016-05-25
 06:16:32"},{"message":"Hi","id":"8291","readBy":"2016-05-25 06:33:52","userID":"332","dateTime":"2016-05-25
 06:17:06"},{"message":"nbfsdbf","id":"8292","readBy":"","userID":"339","dateTime":"2016-05-25 07:03
:44"},{"message":"jsdhfjsdhf","id":"8293","readBy":"","userID":"339","dateTime":"2016-05-25 07:06:55"
},{"message":"fbsnf","id":"8294","readBy":"","userID":"339","dateTime":"2016-05-25 07:06:59"},{"message"
:"dfgjdf","id":"8295","readBy":"","userID":"339","dateTime":"2016-05-25 07:08:14"},{"message":"sim","id"
:"8296","readBy":"","userID":"339","dateTime":"2016-05-25 07:24:24"},{"message":"mgmdf,","id":"8297"
,"readBy":"","userID":"339","dateTime":"2016-05-25 10:16:34"},{"message":"bvd","id":"8317","readBy":""
,"userID":"339","dateTime":"2016-05-31 06:25:40"},{"message":"fdfd","id":"8318","readBy":"","userID"
:"339","dateTime":"2016-05-31 06:25:43"}]

I want to show these messages in chat box but date wise like first show date 25 May 2015 then message and all the message related to that date shown in this after new date come its shown that in next time 31 May 2015 message 1 message 2 so on.

And i am printing like this, its simply append the message but i need to filter it with date and time.

for (i = 0; i < msg1.length; i++) { 


    var el = $('<li class="message right appeared"><div class="avatar"><img src="'+url+'/service/getUserImage/'+msg1[i].userID +'/60"/></div><div class="text_wrapper"><div class="text">'+msg1[i].message+'</div></div></li>');
                 $(".chat_window ul").append(el);


                    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Following code can do the grouping by date

var obj = [{
  "message": "fsbdfs",
  "id": "8290",
  "readBy": "2016-05-25 06:17:01",
  "userID": "339",
  "dateTime": "2016-05-25 06:16:32"
}, {
  "message": "Hi",
  "id": "8291",
  "readBy": "2016-05-25 6:33:52",
  "userID": "332",
  "dateTime": "2016-05-25 06:17:06"
}, {
  "message": "nbfsdbf",
  "id": "8292",
  "readBy": "",
  "userID": "339",
  "dateTime": "2016-05-25 07:03:44"
}, {
  "message": "jsdhfjsdhf",
  "id": "8293",
  "readBy": "",
  "userID": "339",
  "dateTime": "2016-05-25 07:06:55"
}, {
  "message": "fbsnf",
  "id": "8294",
  "readBy": "",
  "userID": "339",
  "dateTime": "2016-05-25 07:06:59"
}, {
  "message": "dfgjdf",
  "id": "8295",
  "readBy": "",
  "userID": "339",
  "dateTime": "2016-05-25 07:08:14"
}, {
  "message": "sim",
  "id": "8296",
  "readBy": "",
  "userID": "339",
  "dateTime": "2016-05-25 07:24:24"
}, {
  "message": "mgmdf,",
  "id": "8297",
  "readBy": "",
  "userID": "339",
  "dateTime": "2016-05-25 10:16:34"
}, {
  "message": "bvd",
  "id": "8317",
  "readBy": "",
  "userID": "339",
  "dateTime": "2016-05-31 06:25:40"
}, {
  "message": "fdfd",
  "id": "8318",
  "readBy": "",
  "userID": "339",
  "dateTime": "2016-05-31 06:25:43"
}];

obj.sort(function(a, b) {
  return a.dateTime.localeCompare(b.dateTime)
});

var map = {};
obj.forEach(function(val) {
  var date = val.dateTime.split(" ")[0];
  map[date] = map[date] || [];
  map[date].push(val)
});

console.log(map)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...