How to group time for the same weekday?

I have a question and get stuck to group the time for the weekday? Could anyone please help me? Thanks

This is the input

var array = [{
    monday: true,
    tuesday: true,
    wednesday: true,
    thursday: false,
    friday: false,
    startTime: "10:00",
    endTime: "10:45"
  },
  {
    monday: false,
    tuesday: true,
    wednesday: true,
    thursday: false,
    friday: false,
    startTime: "11:00",
    endTime: "13:00"
  },
  {
    monday: true,
    tuesday: true,
    wednesday: true,
    thursday: false,
    friday: false,
    startTime: "11:00",
    endTime: "12:00"
  },
  {
    monday: true,
    tuesday: true,
    wednesday: true,
    thursday: true,
    friday: true,
    startTime: "8:00"
    endTime: "9:00",
  },
  {
    monday: true,
    tuesday: true,
    wednesday: true,
    thursday: false,
    friday: false,
    startTime: "6:00",
    endTime: "8:00"
  },
  {
    monday: false,
    tuesday: true,
    wednesday: true,
    thursday: false,
    friday: false,
    startTime: "18:00",
    endTime: "19:00"
  }
];

This is the output

var result = [
  {
    weekday: "Monday - Wednesday", 
    time: ["6:00 - 8:00", "10:00 - 10:45", "11:00 - 12:00" ]   
  },
  {
    weekday: "Tuesday - Wednesday", 
    time: ["11:00 - 13:00", "18:00 - 19:00" ]   
  },
  {
    weekday: "Tuesday - Friday", 
    time: ["8:00 - 9:00"]   
  },
];

Answer

You could do something like:

var input = [{ monday: true, tuesday: true, wednesday: true, thursday: false, friday: false, startTime: "10:00", endTime: "10:45" }, { monday: false, tuesday: true, wednesday: true, thursday: false, friday: false, startTime: "11:00", endTime: "13:00" }, { monday: true, tuesday: true, wednesday: true, thursday: false, friday: false, startTime: "11:00", endTime: "12:00" }, { monday: true, tuesday: true, wednesday: true, thursday: true, friday: true, startTime: "8:00", endTime: "9:00", }, { monday: true, tuesday: true, wednesday: true, thursday: false, friday: false, startTime: "6:00", endTime: "8:00" }, { monday: false, tuesday: true, wednesday: true, thursday: false, friday: false, startTime: "18:00", endTime: "19:00" } ];

let result = [];

input.forEach(x => {
   let keys = [];
   Object.entries(x)
         .filter(([key, value]) => value === true)
         .forEach(([key, value]) => keys.push(key));
   let newWeekDay = keys[0] + " - " + keys[keys.length - 1];  
   let interval = [x.startTime + " - " + x.endTime]; 
   if (result.find(j => j.weekday === newWeekDay)) {
     result = result.map(z => {
        if (z.weekday === newWeekDay) z.time.push(...interval);
        return z;
     })
   }
   else result.push({ weekday: newWeekDay, time: interval });
})

console.log(result);

In brief:

  1. find keys with value as true;
  2. collect interval date;
  3. add interval if weekday is already present in result array or create a new object and push it into array.