.filter 是一个内置的数组迭代方法, 它接受一个 "谓词 (译者注: 指代一个过滤条件的函数)", 该 "谓词" 针对每个值进行调用, 并返回一个符合该条件("truthy 值") 的数组.
上面那句话包含了很多信息, 让我们来逐一解答一下.
"内置" 只是意味着它是语言的一部分 - 您不需要添加任何库来访问此功能.
"迭代方法" 是指接受针对数组的每个项运行的函数..map 和. reduce 都是迭代方法的示例.
"谓词" 是指. fiflter 中接受的的函数.
"truthy 值" 是强制转换为布尔值时计算为 true 的任何值. 几乎所有值都是真实的, 除了: undefined,null,false,0,NaN 或 ""(空字符串).
让我们来看看下面这个例子, 看一下. filter 是怎么运行的.
- const restaurants = [
- {
- name: "Dan's Hamburgers",
- price: 'Cheap',
- cuisine: 'Burger',
- },
- {
- name: "Austin's Pizza",
- price: 'Cheap',
- cuisine: 'Pizza',
- },
- {
- name: "Via 313",
- price: 'Moderate',
- cuisine: 'Pizza',
- },
- {
- name: "Bufalina",
- price: 'Expensive',
- cuisine: 'Pizza',
- },
- {
- name: "P. Terry's",
- price: 'Cheap',
- cuisine: 'Burger',
- },
- {
- name: "Hopdoddy",
- price: 'Expensive',
- cuisine: 'Burger',
- },
- {
- name: "Whataburger",
- price: 'Moderate',
- cuisine: 'Burger',
- },
- {
- name: "Chuy's",
- cuisine: 'Tex-Mex',
- price: 'Moderate',
- },
- {
- name: "Taquerias Arandina",
- cuisine: 'Tex-Mex',
- price: 'Cheap',
- },
- {
- name: "El Alma",
- cuisine: 'Tex-Mex',
- price: 'Expensive',
- },
- {
- name: "Maudie's",
- cuisine: 'Tex-Mex',
- price: 'Moderate',
- },
- ];
这是很多信息. 我现在想要一个汉堡, 所以让我们过滤掉一下这个数组.
- const isBurger = ({cuisine}) => cuisine === 'Burger';
- const burgerJoints = restaurants.filter(isBurger);
isBurger 是谓词, 而 burgerJoints 是 new 数组, 它是餐馆的子集. 值得注意的是, restaurants 这个数组是不变.
下面是两个正在呈现的列表的简单示例 - 一个原始的餐馆数组, 以及一个过滤的 burgerJoints 数组.
See the Pen .filter - isBurger https://codepen.io/AdamGiese/pen/ajLZNz/ by Adam Giese (@AdamGiese https://codepen.io/AdamGiese ) on CodePen https://codepen.io .
否定谓词
对于每个谓词, 都有一个相反的否定谓词.
谓词是一个返回布尔值的函数. 由于布尔值只有 true 和 false, 这意味着很容易 "翻转" 谓词的值.
我吃了汉堡已经过了几个小时, 现在又饿了. 这一次, 我想过滤 out 汉堡尝试新的东西. 一种选择是从头开始编写新的 isNotBurger 谓词.
- const isBurger = ({cuisine}) => cuisine === 'Burger';
- const isNotBurger = ({cuisine}) => cuisine !== 'Burger';
但是, 请查看两个谓词之间的相似程度. 这不是 DRY code https://en.wikipedia.org/wiki/Don't_repeat_yourself . 另一种选择是调用 isBurger 谓词并翻转结果.
- const isBurger = ({cuisine}) => cuisine === 'Burger';
- const isNotBurger = restaurant => !isBurger(restaurant);
这个更好! 如果汉堡的定义发生变化, 您只需要在一个地方更改逻辑. 但是, 如果我们想要一些否定的谓词呢? 由于这是我们可能经常想要做的事情, 因此编写否定函数可能是个好主意.
- const negate = predicate => function() {
- return !predicate.apply(null, arguments);
- }
- const isBurger = ({cuisine}) => cuisine === 'Burger';
- const isNotBurger = negate(isBurger);
- const isPizza = ({cuisine}) => cuisine === 'Pizza';
- const isNotPizza = negate(isPizza);
你可能有一些问题.
什么是. apply?
MDN: https://developer.mozilla.org/en-US/docs/web/JavaScript/Reference/Global_Objects/Function/apply
apply()方法调用具有给定 this 的函数, 并将参数作为数组 (或类数组对象) 提供.
什么是 arguments?
MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments
arguments 对象是所有 (非箭头) 函数中可用的局部变量. 您可以使用参数在函数内引用函数的参数 object.
为什么要使用旧的 function, 而不使用更酷的箭头函数?
在这种情况下, 使用传统函数是必要的, 因为 arguments 对象在传统函数上是_唯一_可用的.
到 2018 年 8 月 20 日. 正如一些评论家所正确指出的那样, 你可以使用 rest 参数 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters 用箭头函数写 \ negate \ https://CSS-tricks.com/level-up-your-filter-game/#comment-1651631 .
返回谓词
正如我们在使用 negate 函数看到的那样, 函数很容易在 JavaScript 中返回一个新函数. 这对于编写 "谓词" 非常有用. 例如, 让我们回顾一下我们的 isBurger 和 isPizza 谓词.
- const isBurger = ({cuisine}) => cuisine === 'Burger';
- const isPizza = ({cuisine}) => cuisine === 'Pizza';
这两个谓词具有相同的逻辑; 他们只是在比较上有所不同. 因此, 我们可以将共享逻辑包装在 isCuisine 函数中.
- const isCuisine = comparison => ({cuisine}) => cuisine === comparison;
- const isBurger = isCuisine('Burger');
- const isPizza = isCuisine('Pizza');
现在, 如果我们想开始检查价格怎么办?
- const isPrice = comparison => ({price}) => price === comparison;
- const isCheap = isPrice('Cheap');
- const isExpensive = isPrice('Expensive');
现在 isCheap 和 isExpensive 都是 DRY(译者注: Don't repeat yourself , 一种编程原则, 不也要写重复的代码),isPizza 和 isBurger 都是 DRY, 但 isPrice 和 isCuisine 可以公用他们的逻辑!
- const isKeyEqualToValue = key => value => object => object[key] === value;
- // these can be rewritten
- const isCuisine = isKeyEqualToValue('cuisine');
- const isPrice = isKeyEqualToValue('price');
- // these don't need to change
- const isBurger = isCuisine('Burger');
- const isPizza = isCuisine('Pizza');
- const isCheap = isPrice('Cheap');
- const isExpensive = isPrice('Expensive');
对我来说, 这就是箭头功能之美. 在一行中, 您可以优雅地创建三阶函数.
看看从原始餐馆阵列创建多个筛选列表是多么容易?
See the Pen .filter - returning predicates https://codepen.io/AdamGiese/pen/mjqvdo/ by Adam Giese (@AdamGiese https://codepen.io/AdamGiese ) on CodePen https://codepen.io .
撰写谓词
我们现在可以通过汉堡或廉价的价格过滤我们的阵列...... 但是如果你想要 cheap burgers 怎么办? 一种选择是将两个过滤器链接在一起.
const cheapBurgers = restaurants.filter(isCheap).filter(isBurger);
另一个选择是将两个谓词 "组合" 成一个谓词.
- const isCheapBurger = restaurant => isCheap(restaurant) && isBurger(restaurant);
- const isCheapPizza = restaurant => isCheap(restaurant) && isPizza(restaurant);
看看所有重复的代码. 我们绝对可以将它包装成一个新功能!
- const both = (predicate1, predicate2) => value =>
- predicate1(value) && predicate2(value);
- const isCheapBurger = both(isCheap, isBurger);
- const isCheapPizza = both(isCheap, isPizza);
- const cheapBurgers = restaurants.filter(isCheapBurger);
- const cheapPizza = restaurants.filter(isCheapPizza);
如果你没有披萨或汉堡包怎么办?
- const either = (predicate1, predicate2) => value =>
- predicate1(value) || predicate2(value);
- const isDelicious = either(isBurger, isPizza);
- const deliciousFood = restaurants.filter(isDelicious);
这是朝着正确方向迈出的一步, 但是如果您想要包含两种以上的食物呢? 这不是一种可扩展的方法. 有两种内置的数组方法在这里派上用场..every 和. some 都是谓词方法, 也接受谓词..every 检查数组的每个成员是否传递谓词, 而. some 检查数组的 any 成员是否通过谓词.
- const isDelicious = restaurant =>
- [isPizza, isBurger, isBbq].some(predicate => predicate(restaurant));
- const isCheapAndDelicious = restaurant =>
- [isDelicious, isCheap].every(predicate => predicate(restaurant));
并且, 像往常一样, 让我们将它们包装成一些有用的抽象.
- const isEvery = predicates => value =>
- predicates.every(predicate => predicate(value));
- const isAny = predicates => value =>
- predicates.some(predicate => predicate(value));
- const isDelicious = isAny([isBurger, isPizza, isBbq]);
- const isCheapAndDelicious = isEvery([isCheap, isDelicious]);
isEvery 和 isAny 都接受一个谓词数组并返回一个谓词.
由于所有这些谓词都可以通过高阶函数轻松创建, 因此根据用户的交互创建和应用这些谓词并不困难. 综合我们学到的所有课程, 这里是一个应用程序示例, 通过应用基于按钮点击的过滤器来搜索餐馆.
See the Pen .filter - dynamic filters https://codepen.io/AdamGiese/pen/xJPMrJ/ by Adam Giese (@AdamGiese https://codepen.io/AdamGiese ) on CodePen https://codepen.io .
总结
过滤器是 JavaScript 开发的重要组成部分. 无论您是从 API 响应中挑选出错误数据还是响应用户交互, 您都会无数次想要数组值的子集. 我希望这个概述有助于您可以操作谓词来编写更易读和可维护的代码.
来源: https://juejin.im/entry/5b8671b851882542ac49528e