0

Within my app I'm trying to develop the ability to filter my returned array of offers if they fall within a set of dates set using a datepicker.

My datepicker emits the values to two properties within a range object - this is filters.range.startDate & filters.range.endDate. Each offer in my array has the properties, offer.dates.start & offer.dates.end.

I've added the below statement in my computed property which doesn't break the computed, just returns no results regardless of dates.

Does anyone have any advice?

EDIT- Added the entire computed property with the date range statement as the last condition.

    computed: {
    filteredOffers() {
     let filtered = this.offers.filter(offer => {
        return (offer.island === this.filters.islandFilter || this.filters.islandFilter === 'All') // Island 
           && (offer.starrating === this.filters.starRating || this.filters.starRating === 'All') // Star Rating 
          && (offer.board === this.filters.boardBasis || this.filters.boardBasis === 'All') // Board Basis
          && (offer.duration === this.filters.duration || this.filters.duration === 'All') // Duration 
          && (offer.price.from < this.filters.price) // Price
          && (this.filters.travelby === 'sea' && offer.travel.air === false || this.filters.travelby === 'All') // Sea or Air
      && (this.filters.range.startDate >= offer.dates.start && offer.dates.end <= this.filters.range.endDate) // DATE RANGE!!
          });

    if (this.sortby === 'ascending') {
      return filtered.sort((a, b) => {
        return a.price.from - b.price.from;
      })
    } else {
      return filtered.sort((a, b) => {
        return b.price.from - a.price.from;
      })
    }
  }
}
11
  • If I were you, I would use a method to calculate this. Could you show us more code? Commented Jan 24, 2019 at 15:19
  • Thanks Raphael - I've added my full computed code above which returns and filters my offers array. Commented Jan 24, 2019 at 15:29
  • If you remove this date range filter, it works? Commented Jan 24, 2019 at 15:34
  • Yes, the full array returns. Here is the two screenshots, one with the date range filter commented out which works. (imgur.com/a/o2UjN3f) Commented Jan 24, 2019 at 15:47
  • 1
    If I were you, I would transform my dates to timestamp in milisecond to compare. Commented Jan 24, 2019 at 16:26

1 Answer 1

1

First, I would transform your date objects to timestamp in milliseconds, just avoid some format errors when you compare.

let date = new Date();
let timestamp = date.getTime();

After that, I guess your logic is not correct, because your end date on filter should be greater than your offer end date, and your start date on filter should be smaller than your offer start date.

this.filters.range.startDate <= offer.dates.start && this.filters.range.endDate >= offer.dates.end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.