[英]How to sort the API data according to date in JavaScript?
I am successfully retrieving the information from an API call but I was required to sort the data according to dates.我成功地从 API 调用中检索信息,但我需要根据日期对数据进行排序。 Everything is perfect but when retrieving the data from API the data is not according to dates.
一切都很完美,但是从 API 检索数据时,数据不符合日期。
function retrieveInfo(container){
var user = userLoggedIn;
console.log(user);
container.html();
fetchNext5Days();
dates.forEach(async (date) =>{
url= API ;
await fetch(url).then(async(res)=>{
return await res.json();
}).then(async (data)=>{
data.centers.forEach(async (center)=>{
var html = await createInfoFeedHtml(center);
container.append(html);
});
})
})}
fetchNext5days function fetchNext5days function
async function fetchNext5Days(){
let today = moment();
for(let i = 0 ; i < 5 ; i ++ ){
let dateString = today.format('DD-MM-YYYY')
dates.push(dateString);
console.log(dates)
today.add(1, 'day');
}
}
This function is also outputting the dates correctly from ascending to descending order.这个 function 也正确地从升序到降序输出日期。 10-05-2021 11-05-2021 12-05-2021 13-05-2021 14-05-2021
10-05-2021 11-05-2021 12-05-2021 13-05-2021 14-05-2021
The difference is in outputting the data.不同之处在于输出数据。
function createInfoFeedHtml(center)
{
return `
<tr class= "dark-row">
<td>${center.sessions[0].date}</td>
<td >${center.name}</td>
<td> ${center.address}</td>
<td>${center.sessions[0].vaccine}</td>
<td>${center.sessions[0].min_age_limit}</td>
<td>${center.sessions[0].available_capacity}</td>
</tr>
`
}
The data from the api is made into table rows and inserted into.来自 api 的数据被制成表格行并插入。
Pug file哈巴狗文件
block content
.container
.row
.table-responsive.table-bordered.movie-table
table.table.movie-table
thead
tr.movie-table-head
th Date
th Name
th Address
th Vaccine
th Age
th Availability
tbody
Your async function is returning data at different intervals so your date order is getting lost.您的异步 function 以不同的时间间隔返回数据,因此您的日期顺序丢失了。 Just write the elements to the page and sort them on their dates as they get written.
只需将元素写入页面并在写入时按日期对它们进行排序。 I modified your createInfoFeedHtml function to put a timestamp on the divs
我修改了您的 createInfoFeedHtml function 以在 div 上放置时间戳
dates.forEach(async (date) =>{
....
}).then(async (data)=>{
data.centers.forEach(async (center)=>{
var html = await createInfoFeedHtml(center);
container.append(html);
sortTableOnDate(container);
});
})
})}
function createInfoFeedHtml(center)
{
var timestamp = new Date(center.sessions[0].date).getTime();
return `
<tr class= "dark-row sortable" data-timestamp='${timestamp}'>
<td>${center.sessions[0].date}</td>
<td >${center.name}</td>
<td> ${center.address}</td>
<td>${center.sessions[0].vaccine}</td>
<td>${center.sessions[0].min_age_limit}</td>
<td>${center.sessions[0].available_capacity}</td>
</tr>
`
}
//sortTableOnDate will gather a set of divs and sort them ascendingly from their timestamp attribute
function sortTableOnDate(container) {
$(container).find('.sortable').sort(function(a, b) {
return +a.getAttribute('data-timestamp') - +b.getAttribute('data-timestamp');
})
.appendTo($(container));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.