在 Javascript 中按日期对未知数量的对象进行排序
Sort unknown amount of objects by date in Javascript
所以我遇到了这个问题,我需要将接收的数据显示为显示事件的 JSON。我想按时间对活动进行排序,因为如果 space 是免费的,人们可以在预订活动之前预订活动。我的另一个问题是 JSON 中的对象数量总是未知的,因为人们总是可以向其中添加更多对象。
下面提供的是我的代码,它应该如何显示。
{
"events":[{
"start": "2018-05-29T09:15:00+02:00",
"end": "2018-05-29T10:00:00+02:00",
"summary": "BreakfastOps",
"organizer": "organizers@email.si",
"meet_url": "https://meet.google.com/exampleLink"
}
]
}
事件数组下可以有更多的对象。现在我想按开始时间对这些对象进行排序。这样我就可以按顺序显示了。 Picture of unorganized events.
注意这里列出的事件对象比较多
更新
根据要求,我在此处添加了我的 main.js 代码。
$.getJSON(some kind of url., render)
function render(data){
console.log(data.events.length)
let filteredDate = data.events.filter((item) =>{
let endTime = new Date(item.end);
if(Date.now()<endTime){
return item;
}
});
let filteredDate2 = filteredDate.filter((item2)=>{
let startTime = new Date(item2.start);
if(Date.now()>startTime){
return item2;
}
});
let filteredDate3 = filteredDate.filter((item2)=>{
let startTime = new Date(item2.start);
if(Date.now()<startTime){
return item2;
}
});
console.log(filteredDate.length)
console.log(filteredDate2.length)
let bodybackground = document.getElementById('body');
let positionbackground = document.getElementById('right');
let free_busy= document.getElementById('free_busy');
//To this point, I have removed the outdated events and put not yet finished events in a new array
if (filteredDate.length>0 && filteredDate2.length===0 ){
bodybackground.classList.add('body_free');
positionbackground.classList.add('right_booked');
free_busy.innerHTML= `<p class=free_busy>FREE</p>`
for(let x=0;x<filteredDate.length;x++){
document.getElementById('event_output').innerHTML=
`${filteredDate.map(eventMap).join("")}`
function eventMap(event){
return `<div class="event">
<h1> Booked meeting:</h1>
<ul>
<li>${event.start} - ${event.end}</li>
<li>${event.summary}</li>
<li>${event.organizer}</li>
<li>${event.meet_url}</li>
</ul>
</div>`
}
}
//document.getElementById('duration').innerHTML = 'FOR ' + DATE.end - DATE.start + ' MINUTES'; this is the next idea i am going to add.
}
//Here it displays free but will show booked meetings.
else if (filteredDate.length > 0 && filteredDate2.length > 0){
bodybackground.classList.add('body_busy');
positionbackground.classList.add('right_inProgress');
free_busy.innerHTML = `<p class=free_busy>BUSY</p>`
document.getElementById('event_output').innerHTML=
`
<div class="event">
<h1 align=center> Meeting in progress:</h1>
<ul>
<li>${filteredDate2[0].start} - ${filteredDate2[0].end}</li>
<li>${filteredDate2[0].summary}</li>
<li>${filteredDate2[0].organizer}</li>
<li>${filteredDate2[0].meet_url}</li>
</ul>
</div><hr color= '#C02D2D' size="20px"><h1 align=center>Other Bookings:</h1>
`+
filteredDate3.map(something).join("")
function something (eventos){
return `<div class="event">
<h1> Booked meeting:</h1>
<ul>
<li>${eventos.start} - ${eventos.end}</li>
<li>${eventos.summary}</li>
<li>${eventos.organizer}</li>
<li>${eventos.meet_url}</li>
</ul>
</div>`
}
}
//Here it will display the Meeting in progress + other meetings booked
else{
bodybackground.classList.add('body_free');
positionbackground.classList.add('right_free');
free_busy.innerHTML= `<p class=free_busy>FREE</p>`
document.getElementById('event_output').innerHTML = `<p class='noevent'>No events booked<p>`
}
//Here it displays a free room and no meetings booked
}
/* Displays current date*/
var date = document.getElementById('date')
var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()
if (minutes < 10){
minutes = "0" + minutes
}
date.innerHTML = (hours + ':'+minutes+" "+ month + "/" + day + "/" + year)
因为您必须对 JSON 中表示为字符串的日期值进行排序。因此,您需要使用 new Date()
对其进行转换,然后获取可用于对数组中的对象进行排序的总时间值。像这样:
var data = {
"events":[
{
"start": "2018-05-29T09:15:00+02:00",
"end": "2018-05-29T10:00:00+02:00",
"summary": "BreakfastOps",
"organizer": "organizers1@email.si",
"meet_url": "https://meet.google.com/exampleLink"
},
{
"start": "2018-04-29T09:15:00+02:00",
"end": "2018-05-29T10:00:00+02:00",
"summary": "BreakfastOps",
"organizer": "organizers2@email.si",
"meet_url": "https://meet.google.com/exampleLink"
},
{
"start": "2018-03-29T09:15:00+02:00",
"end": "2018-05-29T10:00:00+02:00",
"summary": "BreakfastOps",
"organizer": "organizers3@email.si",
"meet_url": "https://meet.google.com/exampleLink"
}
]
};
data.events.sort(function(a, b){
return new Date(a.start).getTime() - new Date(b.start).getTime()
});
console.log(data);
所以我遇到了这个问题,我需要将接收的数据显示为显示事件的 JSON。我想按时间对活动进行排序,因为如果 space 是免费的,人们可以在预订活动之前预订活动。我的另一个问题是 JSON 中的对象数量总是未知的,因为人们总是可以向其中添加更多对象。 下面提供的是我的代码,它应该如何显示。
{
"events":[{
"start": "2018-05-29T09:15:00+02:00",
"end": "2018-05-29T10:00:00+02:00",
"summary": "BreakfastOps",
"organizer": "organizers@email.si",
"meet_url": "https://meet.google.com/exampleLink"
}
]
}
事件数组下可以有更多的对象。现在我想按开始时间对这些对象进行排序。这样我就可以按顺序显示了。 Picture of unorganized events. 注意这里列出的事件对象比较多
更新 根据要求,我在此处添加了我的 main.js 代码。
$.getJSON(some kind of url., render)
function render(data){
console.log(data.events.length)
let filteredDate = data.events.filter((item) =>{
let endTime = new Date(item.end);
if(Date.now()<endTime){
return item;
}
});
let filteredDate2 = filteredDate.filter((item2)=>{
let startTime = new Date(item2.start);
if(Date.now()>startTime){
return item2;
}
});
let filteredDate3 = filteredDate.filter((item2)=>{
let startTime = new Date(item2.start);
if(Date.now()<startTime){
return item2;
}
});
console.log(filteredDate.length)
console.log(filteredDate2.length)
let bodybackground = document.getElementById('body');
let positionbackground = document.getElementById('right');
let free_busy= document.getElementById('free_busy');
//To this point, I have removed the outdated events and put not yet finished events in a new array
if (filteredDate.length>0 && filteredDate2.length===0 ){
bodybackground.classList.add('body_free');
positionbackground.classList.add('right_booked');
free_busy.innerHTML= `<p class=free_busy>FREE</p>`
for(let x=0;x<filteredDate.length;x++){
document.getElementById('event_output').innerHTML=
`${filteredDate.map(eventMap).join("")}`
function eventMap(event){
return `<div class="event">
<h1> Booked meeting:</h1>
<ul>
<li>${event.start} - ${event.end}</li>
<li>${event.summary}</li>
<li>${event.organizer}</li>
<li>${event.meet_url}</li>
</ul>
</div>`
}
}
//document.getElementById('duration').innerHTML = 'FOR ' + DATE.end - DATE.start + ' MINUTES'; this is the next idea i am going to add.
}
//Here it displays free but will show booked meetings.
else if (filteredDate.length > 0 && filteredDate2.length > 0){
bodybackground.classList.add('body_busy');
positionbackground.classList.add('right_inProgress');
free_busy.innerHTML = `<p class=free_busy>BUSY</p>`
document.getElementById('event_output').innerHTML=
`
<div class="event">
<h1 align=center> Meeting in progress:</h1>
<ul>
<li>${filteredDate2[0].start} - ${filteredDate2[0].end}</li>
<li>${filteredDate2[0].summary}</li>
<li>${filteredDate2[0].organizer}</li>
<li>${filteredDate2[0].meet_url}</li>
</ul>
</div><hr color= '#C02D2D' size="20px"><h1 align=center>Other Bookings:</h1>
`+
filteredDate3.map(something).join("")
function something (eventos){
return `<div class="event">
<h1> Booked meeting:</h1>
<ul>
<li>${eventos.start} - ${eventos.end}</li>
<li>${eventos.summary}</li>
<li>${eventos.organizer}</li>
<li>${eventos.meet_url}</li>
</ul>
</div>`
}
}
//Here it will display the Meeting in progress + other meetings booked
else{
bodybackground.classList.add('body_free');
positionbackground.classList.add('right_free');
free_busy.innerHTML= `<p class=free_busy>FREE</p>`
document.getElementById('event_output').innerHTML = `<p class='noevent'>No events booked<p>`
}
//Here it displays a free room and no meetings booked
}
/* Displays current date*/
var date = document.getElementById('date')
var currentTime = new Date()
var month = currentTime.getMonth() + 1
var day = currentTime.getDate()
var year = currentTime.getFullYear()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()
if (minutes < 10){
minutes = "0" + minutes
}
date.innerHTML = (hours + ':'+minutes+" "+ month + "/" + day + "/" + year)
因为您必须对 JSON 中表示为字符串的日期值进行排序。因此,您需要使用 new Date()
对其进行转换,然后获取可用于对数组中的对象进行排序的总时间值。像这样:
var data = {
"events":[
{
"start": "2018-05-29T09:15:00+02:00",
"end": "2018-05-29T10:00:00+02:00",
"summary": "BreakfastOps",
"organizer": "organizers1@email.si",
"meet_url": "https://meet.google.com/exampleLink"
},
{
"start": "2018-04-29T09:15:00+02:00",
"end": "2018-05-29T10:00:00+02:00",
"summary": "BreakfastOps",
"organizer": "organizers2@email.si",
"meet_url": "https://meet.google.com/exampleLink"
},
{
"start": "2018-03-29T09:15:00+02:00",
"end": "2018-05-29T10:00:00+02:00",
"summary": "BreakfastOps",
"organizer": "organizers3@email.si",
"meet_url": "https://meet.google.com/exampleLink"
}
]
};
data.events.sort(function(a, b){
return new Date(a.start).getTime() - new Date(b.start).getTime()
});
console.log(data);