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
。你可以任意混合和旋转它。:-)