简体   繁体   English

如何根据时间戳对对象进行排序?

[英]How to sort an object based on timestamp?

I have a message JSON object where for each message there is a timeStamp of when it was submitted. 我有一个消息JSON对象,其中每个消息都有一个提交时间的时间戳。 I would like to sort that object based on the timeStamp. 我想基于timeStamp对该对象进行排序。

For example: 例如:

 "Messages": { "message1": { "msg" : "I'm trying to make this work", "timeStamp" : "2018-02-15T06:24:44.12+00:00" }, "message2": { "msg" : "I really need your help SO!", "timeStamp" : "2018-03-01T13:57:27+00:00" }, "message3": { "msg" : "Please assist me dude!", "timeStamp" : "2018-03-01T11:57:27+00:00" } } 

The timeStamp generated is from momentjs using moment().format(); 生成时间戳是从momentjs使用moment().format(); .

The issue is, I'm not really sure how to filter an object based on that timestamp format. 问题是,我不太确定如何根据该时间戳格式过滤对象。

I currently have no working example and cant think of a better way. 我目前没有有效的例子,也想不出更好的方法。

Why do I need this? 我为什么需要这个?

I'm using this object to show messages between two people, but the messages need to be in order based on time. 我正在使用此对象显示两个人之间的消息,但是消息需要基于时间排列。

Can't you just change the format and make it simple? 您不能只更改格式并使其简单吗?

No because, currently my whole application is based on that format, and changing it will cause more issues. 否,因为当前我的整个应用程序都基于该格式,因此更改它会引起更多问题。

Update 更新资料

I've forgotten mention that the format of the output must be an object after sorting (where most recent is first item) 我忘了提到输出的格式必须是排序后的对象(最近的是第一项)

This outputs the messages sorted most recent first as an array... you could rebuild the object if you really need to. 这会将最近排序的消息输出为数组...如果您确实需要,可以重建对象。

const json = {
    "Messages": {
        "message1": {
            "msg"       : "I'm trying to make this work",
            "timeStamp" : "2018-02-15T06:24:44.12+00:00"
        },
        "message2": {
            "msg"       : "I really need your help SO!",
            "timeStamp" : "2018-03-01T13:57:27+00:00"  
        },
        "message3": {
            "msg"       : "Please assist me dude!",
            "timeStamp" : "2018-03-01T11:57:27+00:00"
        }
    }
}

const messageIds = Object.keys(json.Messages)

const messages = messageIds.map(id => json.Messages[id]).sort((a, b) =>
    a.timeStamp < b.timeStamp ? 1 : -1
)

console.log(JSON.stringify(messages, null, 4))

you can revive you dates as follows: If you messages are in a variable: foo 您可以按以下方式恢复日期:如果消息在变量中:foo

foo =  {
  "message1": {
    "msg"       : "I'm trying to make this work",
    "timeStamp" : "2018-02-15T06:24:44.12+00:00"
  },
  "message2": {
    "msg"       : "I really need your help SO!",
    "timeStamp" : "2018-03-01T13:57:27+00:00"  
  },
  "message3": {
    "msg"       : "Please assist me dude!",
    "timeStamp" : "2018-03-01T11:57:27+00:00"
  }
}

Then this will add a new property wihch you can sort on (datetime) 然后,这将添加一个新的属性,您可以对其进行排序(日期时间)

foo = Object.keys(foo).map(key => { return Object.assign({}, foo[key], {datetime: new Date(foo[key].timeStamp)}) });

you can get the messages in order as follows: 您可以按以下顺序获取消息:

const sortedMessges = Object.keys(foo).map(key => foo[key]).sort((a, b) =>
    a.datetime< b.datetime ? 1 : -1)

You can sort and then map the sorted array. 您可以sort ,然后map排序后的数组。

 var obj = {"Messages": { "message1": { "msg" : "I'm trying to make this work", "timeStamp" : "2018-02-15T06:24:44.12+00:00" }, "message2": { "msg" : "I really need your help SO!", "timeStamp" : "2018-03-01T13:57:27+00:00" }, "message3": { "msg" : "Please assist me dude!", "timeStamp" : "2018-01-01T11:57:27+00:00" }}}; var result = Object.keys(obj.Messages) .sort((a, b) => Date.parse(obj.Messages[a].timeStamp) - Date.parse(obj.Messages[b].timeStamp)) .map(k => ({ [k]: obj.Messages[k] }) ); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM