Reputation: 115
I have the following multi-dimensional array where slot_date
is coming from the database query. I have a button for every student_id
. But I need only one button for the closest date.
Output in Ajax call
[
'1': {
slot_date: "2019-01-11"
student_id: 26
}
'2': {
slot_date: "2019-01-21"
student_id: 27
}
'3': {
slot_date: "2019-03-11"
student_id: 28
}
'4': {
slot_date: "2019-03-18"
student_id: 29
}
]
Javascript file
join_button = '<a onclick="studentJoinMeeting()"><button class="ongoing-btn">Join Meeting</button></a>';
Desired Output
I need the id of date 2019-03-11
i.e closest date to current date. I need the join button only on that 2019-03-11
date.
Upvotes: 1
Views: 190
Reputation: 13964
You can sort your array based on the dates, by calculating their absolute distance to today. You can then take the first element.
I modified your data array to be a data object since your are using key/values pairs.
const data = {
'1': {
slot_date: "2019-01-11",
student_id: 26
},
'2': {
slot_date: "2019-01-21",
student_id: 27
},
'3': {
slot_date: "2019-03-11",
student_id: 28
},
'4': {
slot_date: "2019-03-18",
student_id: 29
}
};
const today = Date.now();
// This function returns the absolute difference in ms between 2 dates
const dateAbsoluteDiff = (date1, date2) => {
if (date1 - date2 < 0) return date2 - date1;
else return date1 - date2;
};
// get the entries (array of key/values) of the data, sort them based
// on their date distance to today
// take the first element (the closest), this is a key/value pair
// return the second item from this pair, which is the original object
const closest = Object.entries(data).sort((e1, e2) => {
return dateAbsoluteDiff(new Date(e1[1].slot_date), today) -
dateAbsoluteDiff(new Date(e2[1].slot_date), today);
})[0][1];
console.log(closest);
console.log('Student id:', closest.student_id);
If your input data was instead an array, you can sort it directly:
const data = [{
slot_date: "2019-01-11",
student_id: 26
}, {
slot_date: "2019-01-21",
student_id: 27
}, {
slot_date: "2019-03-11",
student_id: 28
}, {
slot_date: "2019-03-18",
student_id: 29
}];
const closest = data.sort((e1, e2) => {
return dateAbsoluteDiff(new Date(e1.slot_date), today) -
dateAbsoluteDiff(new Date(e2.slot_date), today);
})[0];
If instead you want to get the next closest date, then don't take the absolute difference, map your array to an array containing the differences, then sort them and find the first difference greater or equal to 0.
const data = [{
slot_date: "2019-01-11",
student_id: 26
}, {
slot_date: "2019-01-21",
student_id: 27
}, {
slot_date: "2019-03-11",
student_id: 28
}, {
slot_date: "2019-03-18",
student_id: 29
}];
const today = Date.now();
const nextDate = data
.map(({ slot_date }) => ({ slot_date, diff: new Date(slot_date) - today }))
.sort((e1, e2) => e1.diff - e2.diff)
.find(date => date.diff >= 0)
console.log(nextDate.slot_date)
Upvotes: 1
Reputation: 36564
You given array is not valid. Arrays cannot have keys. You cant write key:value
in []
use {}
for that. Here is example with array.
And closest date to current date will the highest date and it will be 2019-03-18
let arr = [{slot_date: "2019-01-11",student_id: 26},
{slot_date: "2019-01-21",student_id: 27},
{slot_date: "2019-03-11",student_id: 28},
{slot_date: "2019-03-18",student_id: 29},
]
let maxDate = 0;
let maxDateID;
for(let item of arr){
if(new Date(item.slot_date) > maxDate){
maxDate = new Date(item.slot_date);
maxDateID = item.student_id;
}
}
Upvotes: 1