Quick question the Locations are always going to be Brentwood, Smyrna, and Spring Hill, is there anyway to always have the list populated with the available times in this order? Right now the locations change based on the available times, meaning that if Smyrna's location has the next available time it will be listed first. –
The link to the JSON file https://www.inquicker.com/facility/americas-family-doctors.json
Here is my fiddle link: http://jsfiddle.net/qUvuB/11/ for the code below, and here is my fiddle link for the code that shows the Locations, Doctor, and Time: jsfiddle.net/mccannf/qUvuB/6/
This is the code I have so far:
I appreciate any help on this.
<!DOCTYPE html>
<html>
<head>
<title>AFD TEST MOD</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script>
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/datejs/1.0/date.min.js"></script>
<script>
$(document).ready(function(){
$.getJSON('https://www.inquicker.com/facility/americas-family-doctors.json',
function(data){
var earliest = {};
var doctor = {};
var links = {};
$.each(data.schedules, function(i, name) {
var location = name.name.split(' - ')[0];
var dr_name = name.name.split(' - ')[1];
if (name.available_times.length) {
if (location in earliest) { // location has already been stored.
var newTime = parseAvailableDate(name.available_times[0].when);
if (newTime.isBefore(earliest[location])) {
earliest[location] = newTime;
doctor[location] = dr_name;
links[location] = name.available_times[0].url;
}
}
else {
earliest[location] = parseAvailableDate(name.available_times[0].when);
doctor[location] = dr_name;
links[location] = name.available_times[0].url;
}
}
});
for (i in earliest) {
$("#names").append("<li><a href='"+links[i]+"'>"+earliest[i].toString("dd/yyyy h:mm tt")+"</a></li>");
}
});
});
function parseAvailableDate(dateString) {
var trimmedString = dateString.replace(/^\s\s*/, '');
var avTime=trimmedString.split(' ')[0],
ampm=trimmedString.split(' ')[1],
avDay=trimmedString.split(' ')[2];
var avDate = Date.parse("next "+avDay);
avDate.addHours(avTime.split(':')[0]).addMinutes(avTime.split(':')[1]);
if (ampm == "pm" && avTime.split(':')[0] != "12") avDate.addHours(12);
return avDate;
}
</script>
</head>
<body>
<ul id="names"></ul>
</body>
</html>
-
1You could certainly help things out by just sorting the JSON needed in the appropriate order that you need for display.Mike Brant– Mike Brant2012年12月11日 20:24:43 +00:00Commented Dec 11, 2012 at 20:24
-
Any ideas on how to do that? I'm up for suggestions, as this has been crazy to get this working.Jason Wallace– Jason Wallace2012年12月11日 20:30:16 +00:00Commented Dec 11, 2012 at 20:30
-
Yes, just edit the file to to put the items in the order you want.Mike Brant– Mike Brant2012年12月11日 20:32:39 +00:00Commented Dec 11, 2012 at 20:32
-
I can't edit the JSON file, as it's coming from another website and their data.Jason Wallace– Jason Wallace2012年12月11日 20:34:59 +00:00Commented Dec 11, 2012 at 20:34
1 Answer 1
Store your locations in an array:
var locations = [];
Then you'll be able to do the sorting easily:
locations.sort();
Of course this will change other parts of your code, but this is standard array manipulation and you should be able to figure it out yourself. For example:
for (i in earliest)
would become (to get the correct order):
for (var i=0;i<locations.length;i++)
or, with jQuery
$.each(locations, function(i,location){...})