0

I've created a filter that checks for events happening today at certain venues using an events API. The filter works, but it seems ripe for refactoring given the repetition of event.venue.name

This is my code:

const tomorrow = moment()
        .add(1, "days")
        .format("LL");

      const optimalVenuesWithEventsTomorrow = eventsData.events.filter(
        event =>
          moment(event.datetime_local).format("LL") === tomorrow &&
          (event.venue.name === "Fenway Park" ||
            event.venue.name === "TD Garden" ||
            event.venue.name === "Charles Playhouse" ||
            event.venue.name === "Citizens Bank Opera House" ||
            event.venue.name === "House of Blues - Boston" ||
            event.venue.name === "Orpheum Theatre" ||
            event.venue.name === "Shubert Theatre")
      );

This seems like a simple problem of moving the parentheses around to adjust the logic. I've tried that, but end up breaking the logic. What am I missing?

Thanks!

elderlyman
  • 500
  • 8
  • 24
  • In similar cases I do two things: 1. `const venueNames = ["fenway park", "td garden", ...];` 2. `venueNames.includes(event.venue.name);` in the filter callback. I think this pattern will keep the values (venue names in this example) easy to refactor, and it can be deemed as configuration – Alex Szabo Jan 25 '20 at 19:44
  • Thanks @AlexSzabó. I'd say your response worked better for me than the one noted by the folks that closed the question out as a duplicate. – elderlyman Jan 25 '20 at 20:00

0 Answers0