0

I have a JSON url file, which I have grabbed the data from and wrote out to an li. The link to the JSON file is https://www.inquicker.com/facility/americas-family-doctors.json. The link to my fiddle file is http://jsfiddle.net/VVnSC/17/. I am trying to narrow down the times to the Next Available time for each location. So I only have 1 time (the next available time) for each location Brentwood, Smyrna, and Spring Hill. I am also trying to get rid of the Location name before the dash in the name so it just displays the person's name. All help will be greatly appreciated, Thanks.

<!DOCTYPE html>
<html>
<head>
<title>AFD TEST</title>
<script type="text/javascript"src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js">   </script>
<script>
$(document).ready(function(){
$.getJSON('https://www.inquicker.com/facility/americas-family-doctors.json',
    function(data){
    console.log(data.facility);
    $.each(data.schedules, function(i, name){
        times=''
            if (name.available_times.length){
                times='<ul>'
                $.each(name.available_times,function(i,o){
                    for (var i; i < 1; i++) {
                    times+='<li><a href="'+ name.available_times[0].url +'">'+o.when+'</a></li>'
                    }
                })
                times+='</ul>'
            }
            else{
               times='<ul><li>No Time Available</li></ul>'                    
            }
        $('#names').append('<li>' + (name.name) + times + '</li>');
    });
});
});
</script>
</head>
<body>
<ul id="names"></ul>
</body>
</html>
4

3 回答 3

0

You should use regex. Something like

var regexp = /- (.*)/g;
var nameShort = regexp.exec(name.name);

The name will then be located in nameShort[1]

so in your code replace

$('#names').append('<li>' + (name.name) + times + '</li>');

with

var regexp = /- (.*)/g;
var nameShort = regexp.exec(name.name);
$('#names').append('<li>' + (nameShort[1]) + times + '</li>');
于 2012-12-11T16:04:05.867 回答
0

Using regexp is the better option if there is a pattern

Just change this code snippet

$('#names').append('<li>' + (name.name) + times + '</li>');

with this

$('#names').append('<li>' + (name.name.replace(/.*?-\s/,'')) + times + '</li>');

Try this and I have update here

http://jsfiddle.net/VVnSC/47/
于 2012-12-11T16:21:19.680 回答
0

You will have to do some fancy date & time comparison, parsing and comparing the days and times.

The following parses the times and dates (see this fiddle for how it works). This also requires date.js (it is added as a resource in the fiddle):

$(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("<ul>Location: "+i+"<li>Doctor: "+doctor[i]+"</li><li>Time: <a href='"+links[i]+"'>"+earliest[i].toString("dddd dd MMMM yyyy H:mm tt")+"</a></li></ul>");         
            }
    });
});

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;
}   

Edit

If the locations are fixed and you have an order in mind, just change this:

            for (i in earliest) {
                $("#names").append("<ul>Location: "+i+"<li>Doctor: "+doctor[i]+"</li><li>Time: <a href='"+links[i]+"'>"+earliest[i].toString("dddd dd MMMM yyyy H:mm tt")+"</a></li></ul>");         
            }

to:

            displayDetails("Brentwood", earliest, doctor, links);
            displayDetails("Smyrna", earliest, doctor, links);
            displayDetails("Spring Hill", earliest, doctor, links);

and create this function (see another fiddle):

   function displayDetails(location, earliest, doctor, links) {
            $("#names").append("<ul>Location: "+location+"<li>Doctor: "+doctor[location]+"</li><li>Time: <a href='"+links[location]+"'>"+earliest[location].toString("dddd dd MMMM yyyy H:mm tt")+"</a></li></ul>");         
   }
于 2012-12-11T17:28:16.950 回答