如何按日期对Json进行排序(最新到最旧)

编程入门 行业动态 更新时间:2024-10-24 13:30:50
本文介绍了如何按日期对Json进行排序(最新到最旧)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个带有文章的json提要(每隔几天就会有新的提要),我需要获取最新的三个.问题是,json不按日期排序,我也不知道如何按日期将其从新到旧排列.我使用js/jQuery.我的json文件看起来像这样:

i have a json feed with articles in it (and new coming every few days) and i need to get latest three out. The problem is, that json is not sorted by date and i don't know, how to rearrange it newest to oldest by date. I use js/jQuery. My json file looks like this:

[ { "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) . ]

推荐答案

您可以使用 Array.sort()函数.像这样:

You can use Array.sort() function. Something like this:

(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; })); })();

更多推荐

如何按日期对Json进行排序(最新到最旧)

本文发布于:2023-10-27 23:32:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1534857.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:新到   按日   Json

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!