skip to Main Content

I am new to coding, to js and GAS particularly. I am writing a script to find the day that has the most events in a calendar and I have encountered a problem when i can’t access certain properties in my array. I have this part of the code in google apps script:

      let startDate = events[i].getStartTime(); 
      let daysRange = getDatesBetween(startDate, realEndDate).toLocaleString('default', { weekday: 'long' });
      const daysArr = daysRange[0];
      Logger.log(daysArr);

I want daysArr to return the whole word, but it only returns the first letter. Could anyone please help me understand why is it returning only letter and not the whole word?

P.S. getDatesBetween is a custom function that works like this:

function getDatesBetween(startDate, realEndDate) {
  
  let dates = []; //var to store dates between startDate and end Date
  let intDate = new Date(startDate); //initializing intDate with startDate without mutating
    dates.push(intDate);

    //loop while intDate < realEndDate continue the loop (adding days to the array)
   while (realEndDate - intDate > 0) {
   dates.push(new Date(intDate));
   intDate.setDate(intDate.getDate() + 1);
        
  }

  return dates;
  }

startDate and realEndDate use events[i].getStartTime() and events[i].getEndTime()

In conclusion, i feel that the daysRange somehow returns an array that is not suitable for my further operations. So i want to combine all the data in daysRange in an array like this:

daysRange = ["Monday", "Tuesday", "Friday", "Wednesday", "Monday", "Tuesday"];

So that i could then count the most popular word in an array. I tried flat(), join(), split(), map() methods but that didn’t work since daysRange[0] returns a letter and not a whole word, so all of the methods also make operations to letters and not the words.

2

Answers


  1. Chosen as BEST ANSWER

    I managed to resolve my problem using the suggested methods but with different approach. The thing was that i have all these variables inside the loop. So i declared a global variable arrDays outside of the loop in order to store the array in there

    let startDate = events[i].getStartTime(); 
          let daysRange = getDatesBetween(startDate, realEndDate).toLocaleString('default', { weekday: 'long' });
          let splitDays = daysRange.split(",");
          arrDays.push(splitDays); 
    

    Then after the loop, I created another variable in order to combine several small arrays into one big array:

    let realArrDays = arrDays.flat()
    

    And then i could do necessary calculations.

    Thank you for your contribution and willingness to help!


  2. Use filter()

    daysRange = ["Monday, Tuesday", "Tuesday", "Friday", "Wednesday, Thursday", "Monday", "Tuesday"]
        var multipleDays = daysRange.filter(day => day.includes(','));
        console.log(multipleDays);
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search