gosseti
gosseti

Reputation: 975

Filter list of dates with same days in common using ramda

I have two lists of dates. I’d like to be left with a list that contains only the days the two lists have in common. For this I’m thinking to use filter and any to compare the two.

const dates = [
  "2019-05-19T09:00:00.000Z",
  "2019-05-20T17:00:00.000Z",
  "2019-05-21T17:00:00.000Z"
]

const datesToCompare = [
  "2019-05-21T17:00:00.000Z"
]

// when filtered should leave us with:
[
  "2019-05-21T17:00:00.000Z"
]

For each item, I’ll need to compare it using a predicate function from date-fns called isSameDay. (As the name implies, it compares two dates and says if they’re on the same day).

Upvotes: 0

Views: 462

Answers (1)

customcommander
customcommander

Reputation: 18901

You could use innerJoin

Takes a predicate pred, a list xs, and a list ys, and returns a list xs' comprising each of the elements of xs which is equal to one or more elements of ys according to pred.

R.innerJoin(dateFns.isSameDay, dates, datesToCompare);

Example:

const dates = [
  "2019-05-19T09:00:00.000Z",
  "2019-05-20T17:00:00.000Z",
  "2019-05-21T17:00:00.000Z"
]

const datesToCompare = [
  "2019-05-21T17:00:00.000Z"
]

console.log(

  R.innerJoin(dateFns.isSameDay, dates, datesToCompare)

)
<script src="https://cdnjs.cloudflare.com/ajax/libs/date-fns/1.29.0/date_fns.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js"></script>

Upvotes: 4

Related Questions