我想建立一个聊天列表,在其中按未读消息和日期(以时代格式)分组
const myData = {
"4072b701-a799-4951-be60-3f2d461bf482": {
"id": "4072b701-a799-4951-be60-3f2d461bf482",
"from": "BOT",
"to": "b740849b-7553-4ddc-945e-b986aff854bb",
"message": "Glad to connect!",
"timestamp": "2019-06-18T04:30:56.188Z",
"isOwner": false,
"isRead": true
},
"1b2c0e15-a3b3-4994-8fda-e38e3cdc8d05": {
"id": "1b2c0e15-a3b3-4994-8fda-e38e3cdc8d05",
"from": "BOT",
"to": "b740849b-7553-4ddc-945e-b986aff854bb",
"message": "We have offices in San Jose, CA, Malaysia, Singapore and India",
"timestamp": "2019-06-18T04:30:58.189Z",
"isOwner": false,
"isRead": true
},
"9613038a-b78a-4afe-aa2d-71d1a8292ec7": {
"id": "9613038a-b78a-4afe-aa2d-71d1a8292ec7",
"from": "BOT",
"to": "b740849b-7553-4ddc-945e-b986aff854bb",
"message": "Ask away, or select from below options",
"timestamp": "2019-06-18T04:30:58.190Z",
"isOwner": false,
"isRead": true
},
"d409a190-6278-490b-9bf6-f3489b0a1cb5": {
"id": "d409a190-6278-490b-9bf6-f3489b0a1cb5",
"from": "b740849b-7553-4ddc-945e-b986aff854bb",
"to": "BOT",
"message": "this",
"timestamp": "2019-06-18T04:32:55.356Z",
"isOwner": true,
"isRead": true
},
"acfc975f-f522-472d-a1f4-18a737da00f2": {
"id": "acfc975f-f522-472d-a1f4-18a737da00f2",
"from": "BOT",
"to": "b740849b-7553-4ddc-945e-b986aff854bb",
"message": "Glad to connect!",
"timestamp": "2019-06-20T22:50:29.714Z",
"isOwner": false,
"isRead": false
},
"d409a190-6278-490b-9bf6-f3489b0a1cb5": {
"id": "d409a190-6278-490b-9bf6-f3489b0a1cb5",
"from": "b740849b-7553-4ddc-945e-b986aff854bb",
"to": "BOT",
"message": "this",
"timestamp": "2019-06-21T04:32:55.356Z",
"isOwner": true,
"isRead": false
},
};
const newObj = Object.keys(myData)
.map(objId => myData[objId])
.reduce((acc, value) => {
const date = value.timestamp;
if (!acc[date]) {
acc[date] = [];
}
acc[date].push(value);
return acc;
}, {});
console.log(newObj);
如您所见,我仍然没有正确排序时间戳。因此,我错过了对isRead
标志进行分组的操作,而我真的希望date变量采用纪元格式,而只存储日期
我希望这样的输出
{
read: {
1489520157124: [{...}], // this is dummy epoch time
1489520157154: [{...}]
},
unread: {
1489520157124: [{...}], // this is dummy epoch time
1489520157154: [{...}]
}
}
答案 0 :(得分:1)
将read
和unread
属性添加到累加器对象,然后将该对象推入适当的属性。
使用Date.parse()
将时间戳转换为纪元时间。
const myData = {
"4072b701-a799-4951-be60-3f2d461bf482": {
"id": "4072b701-a799-4951-be60-3f2d461bf482",
"from": "BOT",
"to": "b740849b-7553-4ddc-945e-b986aff854bb",
"message": "Glad to connect!",
"timestamp": "2019-06-18T04:30:56.188Z",
"isOwner": false,
"isRead": true
},
"1b2c0e15-a3b3-4994-8fda-e38e3cdc8d05": {
"id": "1b2c0e15-a3b3-4994-8fda-e38e3cdc8d05",
"from": "BOT",
"to": "b740849b-7553-4ddc-945e-b986aff854bb",
"message": "We have offices in San Jose, CA, Malaysia, Singapore and India",
"timestamp": "2019-06-18T04:30:58.189Z",
"isOwner": false,
"isRead": true
},
"9613038a-b78a-4afe-aa2d-71d1a8292ec7": {
"id": "9613038a-b78a-4afe-aa2d-71d1a8292ec7",
"from": "BOT",
"to": "b740849b-7553-4ddc-945e-b986aff854bb",
"message": "Ask away, or select from below options",
"timestamp": "2019-06-18T04:30:58.190Z",
"isOwner": false,
"isRead": true
},
"d409a190-6278-490b-9bf6-f3489b0a1cb5": {
"id": "d409a190-6278-490b-9bf6-f3489b0a1cb5",
"from": "b740849b-7553-4ddc-945e-b986aff854bb",
"to": "BOT",
"message": "this",
"timestamp": "2019-06-18T04:32:55.356Z",
"isOwner": true,
"isRead": true
},
"acfc975f-f522-472d-a1f4-18a737da00f2": {
"id": "acfc975f-f522-472d-a1f4-18a737da00f2",
"from": "BOT",
"to": "b740849b-7553-4ddc-945e-b986aff854bb",
"message": "Glad to connect!",
"timestamp": "2019-06-20T22:50:29.714Z",
"isOwner": false,
"isRead": false
},
"d409a190-6278-490b-9bf6-f3489b0a1cb5": {
"id": "d409a190-6278-490b-9bf6-f3489b0a1cb5",
"from": "b740849b-7553-4ddc-945e-b986aff854bb",
"to": "BOT",
"message": "this",
"timestamp": "2019-06-21T04:32:55.356Z",
"isOwner": true,
"isRead": false
},
};
const newObj = Object.values(myData)
.reduce((acc, value) => {
const date = Date.parse(value.timestamp);
const prop = value.isRead ? "read" : "unread";
if (!acc[prop][date]) {
acc[prop][date] = [];
}
acc[prop][date].push(value);
return acc;
}, {read: {}, unread: {}});
console.log(newObj);