简体   繁体   中英

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).

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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM