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. week4--系统调用的工作机制

    潘恒 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 一.使用库函数AP ...

  2. 小学四则运算APP 第二次冲刺-第二天

    团队成员:陈淑筠.杨家安.陈曦 团队选题:小学四则运算APP 第二次冲刺阶段时间:11.29~12.09 本次发布的判断题功能界面的设置: activity_panduan_set.xml: < ...

  3. java学习--第50天讲到jquery

    4月4日jquery讲完了. jquery组合选择器 逗号隔开 层级选择器 父元素    子元素,直接子元素和间接子元素,空格隔开. 直接后代选择器: 父元素>子元素    选择的直接子元素 下 ...

  4. PAT 甲级 1044 Shopping in Mars

    https://pintia.cn/problem-sets/994805342720868352/problems/994805439202443264 Shopping in Mars is qu ...

  5. Eclipse:An internal error occurred during: "Building workspace". GC overhead limit exceeded

    http://blog.csdn.net/shaozhang872196/article/details/18552273 http://www.cnblogs.com/sonofelice/p/52 ...

  6. error loading midas.dll问题

    如果用的delphi在你的单元里用uses midaslib这个东西就可以把midas静态连接到你的程序楼上的也可以 在程序中使用winexec("regsvr32.exe midas.dl ...

  7. ios微信浏览器中video视频播放问题

    微信ios只支持几种特定的视频格式,一般使用mp4格式的视频(腾讯官方就是用的这种视频格式)

  8. html 響應式web設計

    RWD(響應式web設計)可以根據尺寸大小傳遞網頁,對於平板和移動設備是必須的. <html lang="en-US"> lang表示頁面的主要語言.http://ww ...

  9. Lodop设置文本项行间距、字间距

    LODOP给文本项ADD_PRINT_TEXT设置字间距.行间距,可以在打印设计页面,右键属性里设置,然后在打印设计生成代码,也可以直接写代码.LineSpacing行间距.LetterSpacing ...

  10. Picture POJ - 1177(扫描线求面积并)

    题意:求矩形并的面积.. 解析: 扫描线第一道题....自下而上扫描的... 如果不懂什么是扫描线戳我 #include <iostream> #include <cstdio> ...