js中的filter就是过滤的意思,比如,我们以什么样的方式进行过滤,得到我们想要的结果。

对,我们要的就是这个结果。

给定一个数组,我们要的是Burger(汉堡)

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',
},
];

我们分析这个对象数组,发现cuisine: 'Burger',接下来我们进行求值

const isBurger = ({cuisine}) => cuisine === 'Burger';
const burgerJoints = restaurants.filter(isBurger);

开始的时候我们找了Burger,接下来我们要找除了Burger之的东西

只需要找到的值不是Burger就可以了

const isNotBurger = ({cuisine}) => cuisine !== 'Burger';

从restaurant数组中取就是

const isNotBurger = restaurant => !isBurger(restaurant);

如果我们此时要找Burger和Pizza

那就是

const isBurger = ({cuisine}) => cuisine === 'Burger';
const isPizza = ({cuisine}) => cuisine === 'Pizza';

这两者的共通点是我们可以用一个函数存着他们,然后我们想找哪个的时候,就去调用哪个

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');

我们可以看到当我们找吃的时候和找价格的时候,代码存在一些公共点,此时我们又可以提取成为函数

const isKeyEqualToValue = key => value => object => object[key] === value;
const isCuisine = isKeyEqualToValue('cuisine');
const isPrice = isKeyEqualToValue('price');
const isBurger = isCuisine('Burger');
const isPizza = isCuisine('Pizza');
const isCheap = isPrice('Cheap');
const isExpensive = isPrice('Expensive');

我们我们要找便宜的汉堡那就是

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);

如果想要两种以上的食物就是

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]);

本文提炼自https://zcfy.cc/article/level-up-your-filter-game

by我还差的很远很远

有趣的filter的更多相关文章

  1. FFmpeg filter简介

    [时间:2016-08] [状态:Open] [关键词:FFmpeg, filter, filter graph,命令行] 1. 引言及示例 FFmpeg中的libavfilter提供了一整套的基于f ...

  2. django 操作数据库--orm(object relation mapping)---models

    思想 django为使用一种新的方式,即:关系对象映射(Object Relational Mapping,简称ORM). PHP:activerecord Java:Hibernate C#:Ent ...

  3. 有趣的鼠标悬浮模糊效果总结---(filter,渐变文字)

    绘制渐变背景图 第一种:大神的想法,摘抄 background-image: -webkit-linear-gradient(left, blue, red 25%, blue 50%, red 75 ...

  4. 谈谈一些有趣的CSS题目(三)-- 层叠顺序与堆栈上下文知多少

    开本系列,讨论一些有趣的 CSS 题目,抛开实用性而言,一些题目为了拓宽一下解决问题的思路,此外,涉及一些容易忽视的 CSS 细节. 解题不考虑兼容性,题目天马行空,想到什么说什么,如果解题中有你感觉 ...

  5. 谈谈一些有趣的CSS题目(一)-- 左边竖条的实现方法

    开本系列,讨论一些有趣的 CSS 题目,抛开实用性而言,一些题目为了拓宽一下解决问题的思路,此外,涉及一些容易忽视的 CSS 细节. 解题不考虑兼容性,题目天马行空,想到什么说什么,如果解题中有你感觉 ...

  6. 【CSS进阶】box-shadow 与 filter:drop-shadow 详解及奇技淫巧

    box-shadow 在前端的 CSS 编写工作想必十分常见.但是 box-shadow 除去它的常规用法,其实还存在许多不为人知的奇技淫巧. 喜欢 markdown 版本的可以戳这里. box-sh ...

  7. Python特殊语法--filter、map、reduce、lambda

    一.filter(function, sequence) 对sequence中的item依次执行function(item),将执行结果为True的item组成一个List/String/Tuple( ...

  8. Python特殊语法:filter、map、reduce、lambda [转]

    Python特殊语法:filter.map.reduce.lambda [转] python内置了一些非常有趣但非常有用的函数,充分体现了Python的语言魅力! filter(function, s ...

  9. 一次有趣的XSS漏洞挖掘分析(1)

    最近认识了个新朋友,天天找我搞XSS.搞了三天,感觉这一套程序还是很有意思的.因为是过去式的文章,所以没有图.但是希望把经验分享出来,可以帮到和我一样爱好XSS的朋友.我个人偏爱富文本XSS,因为很有 ...

随机推荐

  1. 没有 iOS 开发者账号的情况下部署到真机的方法

    原文发表于我的技术博客 本文分享了官方推荐的没有 iOS 开发者账号的情况下部署到真机的方法,供参考. 原文发表于我的技术博客 1. 官方推荐的方法 原文在此,也就是 Ionic 官方团队在博客中分享 ...

  2. og标签对SEO的作用及用法

    meta property=og标签对SEO的作用及用法,如果你仔细观察会发现本站点<head>代码中有一段:"property="og:image"这段代码 ...

  3. C. Ehab and a 2-operation task

    链接 [https://codeforces.com/contest/1088/problem/C] 题意 n个数,最多n+1操作,要么前i个数加x,要么前i个数对x取余,最后使得严格递增 分析 直接 ...

  4. B. Equations of Mathematical Magic

    思路 打表找规律,发现结果是,2的(a二进制位为1总数)次方 代码 #include<bits/stdc++.h> using namespace std; #define ll long ...

  5. 续摄影O2O篇

    项目名:摄影O2O 工具:Eclipse ,adt,jdk1.8,MySQL 步骤:(一) 1.导入beauty项目到一个adt中,然后创建模拟器,运行(客户端) 2.导入SocketSever项目到 ...

  6. Android中加解密算法大全

    Base64编码 Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一,本质上是一种将二进制数据转成文本数据的方案,对于非二进制数据,是先将其转换成二进制形式,然后每连续6比特(2的6次 ...

  7. octave基本指令3

    octave基本指令3 数据运算 >> a = [1 2; 3 4; 5 6]; >> b = [11 12; 13 14; 15 16]; >> c = [1 1 ...

  8. Which path should be used jdk or jre for JAVA_HOME environment variable?

    https://stackoverflow.com/questions/17601827/which-one-should-java-home-to-point-jdk-or-jre 临时变更JAVA ...

  9. k8s master 节点加入到可以调配node节点中的命令

    kubectl taint nodes --all node-role.kubernetes.io/master- 应该就可以了  效果再观察 效果为

  10. number (1)eclipse 连接数据库报错 数据库信息不对导致的出错