【发布时间】:2015-11-21 11:05:30
【问题描述】:
在 AJAX 响应中,我得到了具有多个节点的 JSON 数组。如何按时间戳对每个节点进行排序。
我试过的代码:
$$.ajax({
type:'POST',
url: "http://www.xyz.co/get_all_news.php",
dataType: "JSON",
data:{'email': window.localStorage["email"], 'business_id': localStorage.getItem("business_id")},
success: function (jsondata1){
data= JSON.parse(jsondata1);
jsondata1.sort(function(x, y){
return x.created_at - y.created_at;
})
console.log(jsondata1); //Error - Uncaught TypeError: jsondata1.sort is not a function
}
});
jsondata1 的值也是
var jsondata1 = [
{"id":"1","body":"Bsjd djjdjd jdjdkd djjdjd jdjd","votes":"4","update_type":"7","created_at":"2015-11-21 02:03:41","name":"Nehil"},
{"id":"2","body":"#TestingBestNominations","votes":"1","update_type":"7","created_at":"2015-11-21 02:03:44","name":"Nehil"},
{"id":"1","name":"#milestone1","date":"0000-00-00","location":"Mumbai","story_body":"Hdjjdjdbj djfjjd djkdjd","short_link":"A0Ijv","created_at":"2015-11-19 05:09:41","path":"\/SupportData\/ImpalzB2B\/uploads\/90294930451447934978817.jpg","update_type":"3"},
{"id":"1","name":"Product 1","description":"Dbbxbxjjd fjkd","short_link":"CmR0X","created_at":"2015-11-19 05:28:34","path":"\/SupportData\/ImpalzB2B\/uploads\/90294930451447936111369.jpg","update_type":"4"}
]
按(created_at)排序,我得到的错误是
“未捕获的类型错误:jsondata1.sort 不是函数”。
【问题讨论】:
-
什么是
jsondata1在sort之前,什么是data在JSON解析之后在函数中? -
jsondata1的值是我在上面给出的,解析后它会给我每个节点的值 -
如果函数中已经是数组,为什么还要解析呢?
-
试试
new Date(x.created_at) - new Date(y.created_at) -
您可能想在数据上调用
sort()?data.sort()
标签: javascript android jquery arrays json