有没有办法在咖喱函数中参数化过滤器评估的右侧

Is there a way to parameterise the right hand side of a filter evaluation in a curried function

我想转换以下内容以使其可重用/通用。具体来说,我不确定采用什么方法来参数化过滤器评估的右侧。

这是我目前所拥有的,这适用于下面的用例。我正在尝试将柯里化部分转换成类似这样的东西...

const filterProcess = theFilter => theData => theFilter === ${dataBranch}.${dataLeaf}

我当前的工作用例。

const hotelList = [
    {city: "London", hotel: 1},
    {city: "Prague", hotel: 1},
    {city: "London", hotel: 2},
    {city: "Prague", hotel: 2},
]

const isLocated = location => hotels => location === hotels.city

const hotelsIn = hotelList.filter(isLocated(location));

console.log(hotelsIn('London'))

采用迭代方法,因为 hotelsIn 需要是一个函数,所以您希望 isLocated 到 return 一个接受位置的函数:

const hotelList = [
    {city: "London", hotel: 1},
    {city: "Prague", hotel: 1},
    {city: "London", hotel: 2},
    {city: "Prague", hotel: 2},
]

const isLocated = location => hotel => location === hotel.city

const hotelsIn = location => hotelList.filter(isLocated(location));
// −−−−−−−−−−−−−−^^^^^^^^^^^^

console.log(hotelsIn('London'))

然后我们可以通过分解出 属性 名称 (city) 来概括:

const hotelList = [
    {city: "London", hotel: 1},
    {city: "Prague", hotel: 1},
    {city: "London", hotel: 2},
    {city: "Prague", hotel: 2},
]

const isLocated = (name, value) => item => value === item[name]
// −−−−−−−−−−−−−−−^^^^^       ^    ^^^^    ^^^^^     ^^^^^^^^^^

const hotelsIn = (name, location) => hotelList.filter(isLocated(name, location))
// −−−−−−−−−−−−−−^^^^^^^^^^^^^^^^^^^

console.log(hotelsIn('city', 'London'))
// −−−−−−−−−−−−−−−−−−^^^^^^^

如果需要,您可以添加一个 hotelsInCity 函数:

const hotelList = [
    {city: "London", hotel: 1},
    {city: "Prague", hotel: 1},
    {city: "London", hotel: 2},
    {city: "Prague", hotel: 2},
]

const isLocated = (name, value) => item => value === item[name]

const hotelsIn = (name, location) => hotelList.filter(isLocated(name, location))

const hotelsInCity = city => hotelsIn('city', city)

console.log(hotelsInCity('London'))