使用 ramda 过滤具有相同日期的日期列表
Filter list of dates with same days in common using ramda
我有两个日期列表。我想留下一个列表,其中只包含两个列表共有的日子。为此,我正在考虑使用 filter
和 any
来比较两者。
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"
]
对于每个项目,我需要使用 date-fns
中名为 isSameDay
的谓词函数对其进行比较。 (顾名思义,它比较两个日期并判断它们是否在同一天)。
你可以使用 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);
示例:
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>
我有两个日期列表。我想留下一个列表,其中只包含两个列表共有的日子。为此,我正在考虑使用 filter
和 any
来比较两者。
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"
]
对于每个项目,我需要使用 date-fns
中名为 isSameDay
的谓词函数对其进行比较。 (顾名思义,它比较两个日期并判断它们是否在同一天)。
你可以使用 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);
示例:
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>