我有一个带有文章的json feed(每隔几天就有新版本),我需要获得最新的三个。问题是,json没有按日期排序,我不知道,如何按日期重新排列最新到最旧。我用js / jQuery。我的json文件如下所示:
[
{
"author":"some text",
"title":"some text",
"description":"some text",
"image_big":"image link",
"image_small":"image link",
"date":"2015-06-17",
"content":"some text some text some text some text some text",
"category":"category",
"subcategory":[
"some subcategory"
],
"keywords":"keywords,keywords...",
"id":"45654",
"url":"article link"
},
.
. (more articles)
.
]
答案 0 :(得分:0)
您可以使用Array.sort()
功能。像这样:
(function() {
var data = [{
"author": "some text",
"title": "some text",
"description": "some text",
"image_big": "image link",
"image_small": "image link",
"date": "2015-06-17",
"content": "some text some text some text some text some text",
"category": "category",
"subcategory": [
"some subcategory"
],
"keywords": "keywords,keywords...",
"id": "45654",
"url": "article link"
}, {
"author": "some text 2",
"title": "some text 2",
"description": "some text 2",
"image_big": "image link 2",
"image_small": "image link 2",
"date": "2015-06-20",
"content": "some text some text some text some text some text",
"category": "category",
"subcategory": [
"some subcategory"
],
"keywords": "keywords,keywords...",
"id": "45654",
"url": "article link"
}];
console.log(data.sort(function(a, b) {
return b.date > a.date;
}));
})();