Ramda:根据项目数组过滤数组

Ramda: filtering an array against an array of items

我有一个过滤以下数组的工作函数:

const arrayOne = [
    {
        node: {
            caseStudyFields: {
                filterTags: [
                    "Temperature control"
                ]
            }
        }
    },
    {
        node: {
            caseStudyFields: {
                filterTags: null
            }
        }
    },
    {
        node: {
            caseStudyFields: {
                filterTags: [
                    "Specialist manufacturing",
                    "Pharmaceuticals"
                ]
            }
        }
    },
]
const arrayTwo = [
    'Pharmaceuticals',
    'Specialist manufacturing',
    'Temperature control'
]

const fn = n => n.node.caseStudyFields.filterTags && 
n.node.caseStudyFields.filterTags.some(r => arrayTwo.includes(r))
    
return arrayOne.filter(fn)

此代码运行良好,但我想将其转换为 Ramda(为了好玩)。到目前为止,我已经找到了路径,但我已经对一些和包括(Ramda 中的任何内容?)感到困惑?

const filter = R.filter(
        R.pipe(
            R.path(['node', 'caseStudyFields', 'filterTags']),
        )
    );

return filter(arrayOne)

使用R.pathOr获取路径中的值,如果是null则使用return和空数组。这将防止过滤器在遇到 null.

时出错

使用 R.any(Ramda 等价于 Array.some())和 R.includes,与 tags 的数组一起柯里化来查找匹配项:

const { curry, filter, pipe, pathOr, any, includes, __ } = R

const filterByTags = curry((tags, arr) =>  
  filter(pipe(
    pathOr([], ['node', 'caseStudyFields', 'filterTags']),
    any(includes(__, tags))
  ))
(arr))

const arrayOne = [{"node":{"caseStudyFields":{"filterTags":["Temperature control"]}}},{"node":{"caseStudyFields":{"filterTags":null}}},{"node":{"caseStudyFields":{"filterTags":["Specialist manufacturing","Pharmaceuticals"]}}}]
const arrayTwo = ["Pharmaceuticals","Specialist manufacturing","Temperature control"]

const result = filterByTags(arrayTwo, arrayOne)

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.27.1/ramda.js" integrity="sha512-3sdB9mAxNh2MIo6YkY05uY1qjkywAlDfCf5u1cSotv6k9CZUSyHVf4BJSpTYgla+YHLaHG8LUpqV7MHctlYzlw==" crossorigin="anonymous"></script>