根据时间戳组织JSON数据。帮助会很棒

时间:2012-03-27 11:28:22

标签: javascript json timestamp

对于我的生活我无法搞清楚这一点,一些帮助或一个例子将不胜感激!

如何使用java脚本基于“created_time”属性组织此JSON数据?

{"data": {
var time = { "id1" : [   
                                { "firstName" : "John",  // First element
                                  "lastName"  : "Doe",
                                  "create_time"       : "11:10 PM yesterday" },

                                { "firstName" : "Mary",  // Second Element
                                  "lastName"  : "Smith",
                                  "create_time"       : "13 minutes ago" },

                                { "firstName" : "Mary",  // Second Element
                                  "lastName"  : "Smith",
                                  "create_time"       : "8:11 PM yesterday" },

                                { "firstName" : "Mary",  // Second Element
                                  "lastName"  : "Smith",
                                  "create_time"       : "10 hours ago" },

                                { "firstName" : "Mary",  // Second Element
                                  "lastName"  : "Smith",
                                  "create_time"       : "6:51 PM 2 days ago" },

                                { "firstName" : "Mary",  // Second Element
                                  "lastName"  : "Smith",
                                  "create_time"       : "11:40 AM Mar 25th" },

                                { "firstName" : "Mary",  // Second Element
                                  "lastName"  : "Smith",
                                  "create_time"       : "12:01 PM Mar 25th" }

                              ],                                

            }

1 个答案:

答案 0 :(得分:0)

使用Javascript Array.sort方法。

sortByDate(function(a, b){
  var dateA=new Date(a.create_time), dateB=new Date(b.create_time)
  return dateA-dateB //sort by date ascending
})

最后

firstLevel.data.time['id'].sort(sortByDate);