问题描述
问题: 如果日期是银行假期,我如何使用API返回布尔值?
我已经完成了一些研究,找到了一个包含银行假期的伟大和免费API,但我在使用它时遇到了麻烦: http ://haydayapi.com/
如果我要使用此代码:
var year = 2016; var month = 3; var day = 25; var isAHoliday = false; $.getJSON( "http://holidayapi.com/v1/holidays?country=GB&year=" + year + "&month=" + month + "&day=" + day, function (data) { console.log(data); //DOES NOT DISPLAY IN CONSOLE if (data.holidays.length > 0) { // BANK HOLIDAY isAHoliday = true; } else { //IS NOT BANK HOLIDAY //AND NOTHING NEEDS TO BE DONE } });
我希望能够返回一个真或假值,这取决于这是否返回任何数据,然而我正在做错事,因为 getjson请求没有被称为,请有人正确我在哪里出错了?
http://holidayapi.com/v1/haydays?country= GB&年= 2016&月= 03和日= 25 返回{"status":200,"holidays":[{"name":"Good Friday","country":"GB","date":"2016-03-25"}]}
http://holidayapi.com/v1/haydays?country= GB&年= 2016和月= 03和日= 26 返回{"status":200,"holidays":[]}
它看起来这导致问题: "http://holidayapi.com/v1/holidays?country=GB&year=" + year + "&month=" + month + "&day=" + day;如果我通过上面的2个URL之一,我得到了正确的结果,我现在有这个
https://jsfiddle.net/dcxk6ens/
推荐答案
如果您只想返回一个true值,如果所选日期是假期,或false如果不是,则可以使用这样的函数:
(请注意,jsfiddle不会使用"http://"协议执行对URL的任何Ajax调用,因为它不安全.)
function isDateAHoliday(y, m, d) { var jsonURL = "http://holidayapi.com/v1/holidays?country=GB&year=" + y + "&month=" + m + "&day=" + d; var isAHoliday = false; $.getJSON(jsonURL, function (data) { // If the date is a holiday if (data.holidays.length > 0) { // Do some things isAHoliday = true; } // Check values console.log("JSON DATA: ", data); console.log("Holiday?: " + isAHoliday); return isAHoliday; }); } isDateAHoliday("2016", "3", "25");
如果您想要返回假日的名称和国家,您可以替换if语句的内部isAHoliday = data.holidays[0];.
其他推荐答案
holidays对象必须称为返回的data对象的子项:
由于holidays对象是一个数组,您还需要使用索引来访问项目.假设返回至少有一个项目,您将获得这样的日期:
var myDate = data.holidays[0].date;但是,您应该始终检查阵列中至少有一个对象在获得第一个:
if(data.holidays.length > 0){...}顺便提一下,如果您想要做的只是在任何特定日期都有假期,那么这个if语句就是你需要的,因为数组长度超过零意味着至少有一个假期.
编辑
对您的问题进行完整答案,您可以将其放在.done()方法中:
var isAHoliday = false; if(data.holidays.length > 0){ // There's at least one holiday today! isAHoliday = true; }
您不必声明一个本地变量,您可能会使用一个在其他地方声明的变量,而是由您宣布.
问题描述
the Question: How can I use the API to return a boolean value if the date is a bank holiday?
I have done some research and found a great, and free API which contains bank holidays, however I am having trouble using it: http://holidayapi.com/
if i was to use this code:
var year = 2016; var month = 3; var day = 25; var isAHoliday = false; $.getJSON( "http://holidayapi.com/v1/holidays?country=GB&year=" + year + "&month=" + month + "&day=" + day, function (data) { console.log(data); //DOES NOT DISPLAY IN CONSOLE if (data.holidays.length > 0) { // BANK HOLIDAY isAHoliday = true; } else { //IS NOT BANK HOLIDAY //AND NOTHING NEEDS TO BE DONE } });
i want to be able to return a true or false value depending on if this returns any data or not, however im doing something wrong as the getJSON request is not being called, please could someone correct me where i have gone wrong?
http://holidayapi.com/v1/holidays?country=GB&year=2016&month=03&day=25 returns {"status":200,"holidays":[{"name":"Good Friday","country":"GB","date":"2016-03-25"}]}
http://holidayapi.com/v1/holidays?country=GB&year=2016&month=03&day=26 returns {"status":200,"holidays":[]}
it appears this is causing an issue: "http://holidayapi.com/v1/holidays?country=GB&year=" + year + "&month=" + month + "&day=" + day; if i pass one of the 2 URL's in above i get the correct result, I am having a play now with this
https://jsfiddle.net/dcxk6ens/
推荐答案
If you simply want to return a true value if the selected date is a holiday, or false if it is not, you could use a function like this:
(Please note that jsfiddle will not execute any AJAX calls to URLs using the "http://" protocol, since it is not secure.)
function isDateAHoliday(y, m, d) { var jsonURL = "http://holidayapi.com/v1/holidays?country=GB&year=" + y + "&month=" + m + "&day=" + d; var isAHoliday = false; $.getJSON(jsonURL, function (data) { // If the date is a holiday if (data.holidays.length > 0) { // Do some things isAHoliday = true; } // Check values console.log("JSON DATA: ", data); console.log("Holiday?: " + isAHoliday); return isAHoliday; }); } isDateAHoliday("2016", "3", "25");
If you wanted to return the name and country of the holiday as well, you could substitute isAHoliday = data.holidays[0]; inside of the if statement.
其他推荐答案
The holidays object must be called as a child of the returned data object:
Since the holidays object is an array you'll also need to use an index to access an item. Assuming there is at least one item returned, you would get the date like so:
var myDate = data.holidays[0].date;
However you should always check that there's at least one object in the array before getting the first one:
if(data.holidays.length > 0){...}
Incidentally, if all you want to do is check if there's a holiday on any particular day then this if statement is all you'll need, since an array length of more than zero means there's at least one holiday.
Edit
A full answer to your question, you could put this inside the .done() method:
var isAHoliday = false; if(data.holidays.length > 0){ // There's at least one holiday today! isAHoliday = true; }
You don't have to declare a local variable, you'll probably use one that's declared elsewhere but that's up to you.