简体   繁体   English

使用ramda过滤具有相同日期的日期列表

[英]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. 为此,我正在考虑使用filterany来比较两者。

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 . 对于每个项目,我需要使用date-fns isSameDay的谓词函数isSameDay (As the name implies, it compares two dates and says if they're on the same day). (顾名思义,它比较两个日期并说它们是否在同一天)。

You could use innerJoin 您可以使用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. 取谓词pred,列表xs和列表ys,并根据pred返回包含xs的每个元素的列表xs',这些元素等于ys的一个或多个元素。

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> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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