Finding date closest to today from array of objects

Clash Royale CLAN TAG#URR8PPP
Finding date closest to today from array of objects
I have parsed a CSV that slices like this:
data[0]
>>Date:08/23/2018, Organizer:RadioShack, Event:Promotion
I found a somewhat helpful question in: Find closest date in array with JavaScript, however I had trouble adapting it from an array of dates to dates stored within an array of objects. I have dates from data[0] to data[10]. It's not a terribly long dataset, however I would like to code it better than my current crude approach:
data[0]
data[10]
var dateArray = ;
dateArray[0] = data[0].Date;
dateArray[1] = data[1].Date;
…
console.log(dateArray.filter(function(d) return d - today > 0; ))
That gives me all dates after today's date, which is a start, but I'm still falling short on finding the date closest to today.
Question: I thought maybe I could just write a for loop to give me that array faster, but I still wonder: is there a way I can find the date closest to today's date from an array of objects and store that date in:
var closestEventDate = ???
@KooiInc - That's rather more work than necessary.
– T.J. Crowder
Aug 12 at 10:19
1 Answer
1
You could reduce your dateArray to the date closest to today:
const today = new Date();
const closest = data.reduce((a, b) => a.Date - today < b.Date - today ? a : b);
Live Example:
var now = Date.now();
var ONE_DAY_IN_MS = 86400000;
function randomDate()
return new Date(now + (5 * ONE_DAY_IN_MS - Math.round(Math.random() * 10 * ONE_DAY_IN_MS)));
var data = [
Date: randomDate(),
Date: randomDate(),
Date: randomDate(),
Date: randomDate(),
Date: randomDate(),
Date: randomDate(),
Date: randomDate(),
Date: randomDate(),
Date: randomDate()
];
console.log("Entries:");
data.forEach(function(entry)
console.log(entry.Date.toISOString());
);
const today = new Date();
const closest = data.reduce((a, b) => a.Date - today < b.Date - today ? a : b);
console.log("today", today.toISOString());
console.log("closest", closest.Date.toISOString());
.as-console-wrapper
max-height: 100% !important;
Or the closest after today:
const today = new Date();
const closest = data.reduce((a, b) =>
const adiff = a.Date - today;
return adiff > 0 && adiff < b.Date - today ? a : b;
);
Live Example:
var now = Date.now();
var ONE_DAY_IN_MS = 86400000;
function randomDate()
return new Date(now + (5 * ONE_DAY_IN_MS - Math.round(Math.random() * 10 * ONE_DAY_IN_MS)));
var data = [
Date: randomDate(),
Date: randomDate(),
Date: randomDate(),
Date: randomDate(),
Date: randomDate(),
Date: randomDate(),
Date: randomDate(),
Date: randomDate(),
Date: randomDate()
];
console.log("Entries:");
data.forEach(function(entry)
console.log(entry.Date.toISOString());
);
const today = new Date();
const closest = data.reduce((a, b) =>
const adiff = a.Date - today;
return adiff > 0 && adiff < b.Date - today ? a : b;
);
console.log("today", today.toISOString());
console.log("closest", closest.Date.toISOString());
.as-console-wrapper
max-height: 100% !important;
Nice to see a valid use of
reduce!– T.J. Crowder
Aug 12 at 10:31
reduce
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Sort the dates in the filtered Array ascending and take the first date from it
– KooiInc
Aug 12 at 10:17