gpt4 book ai didi

javascript - 按日期对 jQuery Ajax Json forEach 数据进行排序

转载 作者:行者123 更新时间:2023-11-28 04:27:54 25 4
gpt4 key购买 nike

我正在从 JSON 文件获取数据。但我的代码读取并附加它,因为它写入文件中,我想按日期对数据进行排序。

我尝试过这个:但不起作用=/

$('.1article').sort(function (a, b) {
var contentA =parseInt( $(a).attr('data-date'));
var contentB =parseInt( $(b).attr('data-date'));
return (contentA < contentB) ? -1 : (contentA > contentB) ? 1 : 0;
})

我的 Ajax 代码

$(document).ready(function() { 
function imgError(image) {
image.onerror = "";
image.src = "img/S0-voiture-de-l-annee-2017-les-sept-finalistes-nos-pronostics-391613.jpg";
return true;
}
$.ajax({
type: "GET",
url: 'http://s604712774.onlinehome.fr/videdressing/text.json',
dataType: 'json',
success: function(data) {
data.forEach(function(post) {
var fds = new Date(post.date) ;

var datetimeposted = new Date(post.date);
var datetime = datetimeposted.getDate() + "/" + (datetimeposted.getMonth()+1) + "/" + datetimeposted.getFullYear();

var currentdate = new Date();
var now = new Date(currentdate.getFullYear() + ", " + ( '0' +(currentdate.getMonth()+1)).slice( -2 ) + ", " + ( '0' +currentdate.getDate()));
var result = (now - fds) ;
var days = result / 1000 / 60 / 60 / 24 ;

if ("14" < days) {
$(".id" + post.id).css('display','none');
};

$( '.articles' ).prepend( ' <div class="1article col s12 m6 l4 id' + post.id + '" data-date="' + fds +'"> <div class="card"> <div class="card-content"> <span class="titlevente activator grey-text text-darken-4 titlevente">' + post.title +'</span><br><span class="titlevente activator grey-text text-darken-4 titlevente" style="font-size:10px !important;">' + post.category +' | ' + datetime +'</span> </div><div class="card-image waves-effect waves-block waves-light"> <img class="activator" src="img/S0-voiture-de-l-annee-2017-les-sept-finalistes-nos-pronostics-391613.jpg"> </div><div class="card-content"> <span class="metadesc activator grey-text text-darken-4">' + post.ville +' | ' + post.prix +'€<i class="material-icons right">more_vert</i></span> </div><div class="card-reveal"> <span class="card-title grey-text text-darken-4">' + post.title +'<i class="material-icons right">close</i></span> <p>' + post.decription +'</p><div class="incarddata"> <i id="delete" class="material-icons h32">&#xE872;</i> <a href=""><i class="material-icons h32">share</i></a> <a href="mailto:' + post.mail +'"><i class="material-icons h32">&#xE158;</i></a> <a href="tel:' + post.telephone +'"><i class="material-icons h32">&#xE0CD;</i></a> </div></div></div></div>' );
});
}
});
});

您可以在这里查找 -> https://jsfiddle.net/ds77bmu9/1/

最佳答案

我编辑了你的 fiddle ,你必须在迭代数组之前对数组进行排序。另外,您的排序功能不正确(在javascript中,您可以简单地用 - 减去日期):

success: function(data) {
data.sort(function(a, b) {
return new Date(b.date) - new Date(a.date);
})
foreach(data : ...)
}

你的 fiddle 已经修好了。 https://jsfiddle.net/ds77bmu9/3/

关于javascript - 按日期对 jQuery Ajax Json forEach 数据进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44888035/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com