0

I'm trying to parse the entries in a Google Calendar feed but so far I haven't been able to retrieve the title, I was hoping someone would give me a suggestion. From what I see in firebug I can't understand why the following is not working, since I loop through the entries with the each method like follows:

$.getJSON("https://www.google.com/calendar/feeds/cide.edu_sm151i2pdhu2371vq8hamcver4@group.calendar.google.com/public/full?q="+encodeURI($(this).val()), {"alt" : "json"}, function(data) {
    $.each(data.feed.entry, function(i, entry) {
        var key = entry.gd$when[0].startTime.substr(0, 10);
        if(key in SelectedDates == false) {
            SelectedDates[key] = [];    
        }
        else {
            var titulo = escape(this.title.$t);
            SelectedDates[key].push(titulo);    
        }
    });
});

I define SelectedDates as an array earlier, but whenever I make the request, the SelectedDates array contains the keys alright, but only blank arrays in them, like follows: 2014-02-04 [] I wish I could grasp how to pass the title of the event to the object.

Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
gerardo flores
  • 402
  • 1
  • 6
  • 28
  • You are using Calendar v2 API which was deprecated 3 years ago and was announced to shutdown on November 17th. Consider migrating to the new version: https://developers.google.com/google-apps/calendar/ – luc Nov 13 '14 at 08:43

1 Answers1

2

The problem is that in javascript an empty array == false.

Change this:

if(key in SelectedDates == false) {

to

if(key in SelectedDates === false) {

EDIT:

empty arrays are not falsey but do equate to false when explicitly checked

https://medium.com/@daffl/javascript-the-weird-parts-8ff3da55798e

Jonathan Crowe
  • 5,793
  • 1
  • 18
  • 28
  • 1
    I believe an empty array is truthy. If you type this in your JS console in Chrome, it will spit out "truthy": if ([]) { console.log('truthy'); } – Ian Davis Nov 13 '14 at 00:29
  • try `[] == false` in your console – Jonathan Crowe Nov 13 '14 at 00:30
  • 1
    Now you've stumped me. [] == false does indeed result as true in JS console, but, if ([]) { console.log('truthy'); } renders "truthy". – Ian Davis Nov 13 '14 at 00:33
  • I see that too. Very strange behavior! – Jonathan Crowe Nov 13 '14 at 00:33
  • 1
    Many thanks, it worked, I also realized that the logic was wrong in my code, I had to remove the else statement and leave it only in `var titulo = escape(this.title.$t); SelectedDates[key].push(titulo);` for it to work – gerardo flores Nov 13 '14 at 00:34
  • @IanDavis http://stackoverflow.com/questions/15240452/empty-array-is-false-but-in-if-statement-it-returns-true - this is why apparently – Jonathan Crowe Nov 13 '14 at 00:38