Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/365.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 有没有办法在当前函数中参数化过滤器计算的右侧_Javascript_Functional Programming_Currying - Fatal编程技术网

Javascript 有没有办法在当前函数中参数化过滤器计算的右侧

Javascript 有没有办法在当前函数中参数化过滤器计算的右侧,javascript,functional-programming,currying,Javascript,Functional Programming,Currying,我想将以下内容转换为可重用/通用。具体来说,我不确定采取什么方法来参数化过滤器评估的右侧 这是我到目前为止所做的,它适用于下面的用例。我想把咖喱部分改成这样 const filterProcess = theFilter => theData => theFilter === ${dataBranch}.${dataLeaf} 我当前的工作用例 const hotelList = [ {city: "London", hotel: 1}, {city: "Pragu

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

这是我到目前为止所做的,它适用于下面的用例。我想把咖喱部分改成这样

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
返回一个接受位置的函数:

const hotelList=[
{城市:“伦敦”,酒店:1},
{城市:“布拉格”,酒店:1},
{城市:“伦敦”,酒店:2},
{城市:“布拉格”,酒店:2},
]
const isLocated=location=>hotel=>location==hotel.city
const hotelsIn=location=>hotelList.filter(isLocated(location));
// −−−−−−−−−−−−−−^^^^^^^^^^^^

console.log(hotelsIn('London'))
Hi T.J,非常感谢。这是可行的,也是有道理的。我不是一个专业的开发者,但我会尽可能地适应它。谢谢你快速而简洁的回答。为什么不把这个功能也咖喱一下呢
const hasProperty=name=>value=>obj=>value===obj[name];const isLocated=hasProperty('city');const inLondon=孤岛(‘伦敦’)
@Bergi-呵呵,是的,我也是这么做的,因为
hotelsInCity
。你可以任意混合和旋转它。:-)