How do I filter my array of objects by date?

I have a showAppointments variable which is an object. I then have a loop that initially sets showAppointments[g] (with g being which specific day the appointment is created in), to an array. In the loop I then push data in the form of an object into the showAppointments[g] array.

.ts file

export class CalendarHoursComponent implements OnInit, OnDestroy {
   allDates = []; allMonths = [];
  appointments: IAppointmentsByDate[];
  currentDate: Date = new Date(); firstSunday: Date; 
  month = ["January", "Febuary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
  ticketId: number; ticket_entries: ITicketsView1[]; showAppointments={}; showHours = {}; showRecAppointments={};

load(delta: number) {
    if (delta!=0) this.currentDate = this.addMonths(this.currentDate, delta);
    if (this.isTicketManager()) {
        this.server.getAppointmentsForDateRange(this.userId, this.firstSunday.getMonth() + 1,
        lastSaturday.getDate(), lastSaturday.getFullYear(), this.endMonth(lastSaturday, 34), this.addDays(lastSaturday, 34),
        this.endYear(lastSaturday, 34), r=>  {this.setAppointments(r) });
      });
    } else {
        this.server.getAppointmentsForDateRange(this.userId, this.firstSunday.getMonth() + 1,
        lastSaturday.getDate(), lastSaturday.getFullYear(), this.endMonth(lastSaturday, 34), this.addDays(lastSaturday, 34),
        this.endYear(lastSaturday, 34), r=> this.setAppointments(r));
      });
    }



private setAppointments(i: IAppointmentsByDate[]) {
    this.allDates.forEach(e=> {
      e.forEach(g=> {
        g.setHours(0,0,0,0);
        this.showAppointments[g] = [];
        this.showRecAppointments[g] = [];
          let f = i.filter(e2=> {
            let i2 = new Date(e2.endTime); i2.setHours(0,0,0,0);
            return i2.valueOf() == g.valueOf();
          });
        if(f) {

          f.forEach(e3=> {
            if(e3.recurrance_id > 1) {
              this.showAppointments[g].push({
                id: e3.id,
                text: e3.customer + " " + "Ticket #" + e3.ticket_id + " " + this.datepipe.transform(e3.startTime, 'hh:mm a') + " - " + this.datepipe.transform(e3.endTime, 'hh:mm a' + " Recurring"),
                date: new Date(e3.startTime),
                time: this.datepipe.transform(e3.startTime, 'hh:mm a')
              });
            } else {
              this.showAppointments[g].push({
                id: e3.id,
                text: e3.customer + " " + "Ticket #" + e3.ticket_id + " " + this.datepipe.transform(e3.startTime, 'hh:mm a') +" - " + this.datepipe.transform(e3.endTime, 'hh:mm a'),
                date: new Date(e3.startTime),
                time: this.datepipe.transform(e3.startTime, 'hh:mm a')
              });
            }

          });
          console.log(this.showAppointments[g]);
          this.showAppointments[g].sort((a,b)=> {
            return a.time.localeCompare(b.time);
         });
        }

The idea as I said, is that I am placing data on a calendar for that particular day. So when I console.log(this.showAppointments[g]) I get something like this:

enter image description here

I thought the filtered method would work below since I am comparing date values and returning only the appointments with dates equal to or after the current date which is what I need. But when I console.log it, I get all empty arrays with no objects inside.

this.appointments[g].filter(e=> e.date.valueOf() >= new Date().valueOf())

So, any suggestions on how to fix the problem?

17 thoughts on “How do I filter my array of objects by date?”

  1. 721438 213799magnificent post, extremely informative. I wonder why the other specialists of this sector do not notice this. You need to continue your writing. Im confident, youve a great readers base already! 631647

    Reply
  2. 64933 668159Get started with wales ahead practically every planking. Ones wales truly are a compilation of huge planks 1 certain depth advisors definitely could be the identical towards the entire hull planking however with even larger density to successfully thrust outward beyond the planking. planking 102582

    Reply

Leave a Comment