Home > OS >  Is there a way I could iterate through dates in an array and add them to this object?
Is there a way I could iterate through dates in an array and add them to this object?

Time:10-28

I'm using a jQuery calendar I found online and I'm struggling to add the dates into it. I am able to iterate through the API response and push them to an array. But from there I am struggling to add it to the calendar.

        $(function(){
          $("#calendar").simpleCalendar();
        });

        var dates = [];
        var descriptions = [];

        for(i = 0; i < result['data']['holidays'].length; i  ){
          let date = result['data']['holidays'][i]['date']['iso'];
          dates.push(new Date(date));
          descriptions.push(result['data']['holidays'][i]['description']);
        }

        console.log(dates[34]   " "   descriptions[34]);
                
        $("#calendar").simpleCalendar({       
          // Events displayed
          displayEvent:true,
          // Dates of the events                  
          events: [           
            {
              startDate: dates[1],
              endDate: dates[1],
              summary: descriptions[1]
            }
          ]
        });       

I've tried adding it inside the loop and putting the array key as "i" but this hasn't worked for me.

CodePudding user response:

Fill the events variable inside of you loop, and use it directly on the json.

    $(function(){
      $("#calendar").simpleCalendar();
    });

    var eventsArr = [];

    for(i = 0; i < result['data']['holidays'].length; i  ){
      let date = result['data']['holidays'][i]['date']['iso'];
      eventsArr.push({
          "startDate": new Date(date),
          "endDate"  : new Date(date),
          "summary"  : result['data']['holidays'][i]['description']
      });
    }

    $("#calendar").simpleCalendar({       
      // Events displayed
      displayEvent:true,
      // Dates of the events                  
      events: eventsArr
    });       
  • Related