vanilla

When building Single Page Applications a feature I frequently find myself adding is a simple search filter. Nothing too in depth, I'll just want a text field to be able to quickly filter over items to find specific ones.

构建单页应用程序时,我经常发现自己添加的功能是一个简单的搜索过滤器。 没什么要深入的,我只是希望一个文本字段能够快速过滤项目以找到特定的项目。

I'm going to share what I do, because it's quick to implement, performant enough, and often very helpful.

我将分享我的工作,因为它实现起来很快,性能足够好,而且通常很有帮助。

To start with, this technique assumes that the data you're filtering over is in the form of an array of objects. From there it's all standard libarary array and string methods.

首先,该技术假定您要过滤的数据是对象数组的形式。 从那里开始,所有的标准库数组和字符串方法。

我们的工具 ( Our Tools )

Array.prototype.filter ( Array.prototype.filter )

Array.prototype.filter is how we filter our array.

Array.prototype.filter是我们过滤数组的方式。

As a parameter, it takes a callback that it runs on each item of the array. This callback should return true or false for whether or not it should be a member of the filtered array.

作为参数,它需要在数组的每个项目上运行的回调。 此回调应返回truefalse以确定它是否应为已过滤数组的成员。

let oneToTen = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let sixToTen = a.filter((element, index) =>  a > 5); // return [6, 7, 8, 9, 10];

String.prototype.toLowerCase ( String.prototype.toLowerCase )

String.prototype.toLowerCase is nothing fancy. It takes a string, it turns all the letters to lowercase.

String.prototype.toLowerCase没什么特别的。 它需要一个字符串,所有字母都变成小写。

For this example it's purpose is string normalization. It's what's going to lets us find "The", even if we type in "the" or "tHe".

在此示例中,其目的是字符串标准化。 即使我们键入“ the”或“ tHe”,它也将使我们找到“ The”。

let lowercaseString = "Hello, World!".toLowerCase(); // returns "hello, world!"

In a more complete setup you may need to do much more for your string normalization.

在更完整的设置中,您可能需要为字符串归一化做更多的事情。

###

###

String.prototype.includes ( String.prototype.includes )

String.prototype.includes is the heavy lifter of the filter. You pass it a string and it gives you back true or false to let you know if its a substring of the original string.

String.prototype.includes是过滤器的重物。 您将其传递给一个字符串,它会返回true或false,以告知您它是否是原始字符串的子字符串。

let data = "I don't want to go, I don't want to go. Mr. Stark , please." // returns false.
let tony = data.includes("Mr. Stark"); //returns true
let peter = data.includes("Spiderman"); //return false

一起拉 ( Pulling it all together )

Laying out each of those, it should be pretty clear how it all comes together. What we want to do is as follows:

对每个布局进行布局,应该很清楚它们如何组合在一起。 我们想要做的如下:

  1. Normalize search input.归一化搜索输入。
  2. Normalize field we are searching on.归一化我们正在搜索的字段。
  3. Check if normalized search input is a substring(included) in the field we are searching on.检查规范化搜索输入是否是我们正在搜索的字段中的子字符串(包括)。
  4. Return a new array with only the items where the last statement was true.返回仅包含最后一条语句为true的项目的新数组。

So what does that looks like? Assuming the data looks like below:

那看起来像什么呢? 假设数据如下所示:

let data = [{title: 'Ready Player One',author: 'Ernest Cline',},{title: 'The Circle',author: 'Dave Eggers',},
];

We can do a quick search filter over the authors by doing:

我们可以通过以下方法对作者进行快速搜索:

let substring = "est c";
let filteredData = data.filter(book => book.author.toLowerCase().includes(substring.toLowerCase()) /*
returns [{title: 'Ready Player One',author: 'Ernest Cline'}
]

注意事项 ( Caveats )

This method returns an array of all matchs, regardless of whether or not one is an exact match. If you want an exact match look instead to Array.prototype.find

此方法返回一个包含所有匹配项的数组,而不管一个匹配项是否完全匹配。 如果您想要完全匹配,请查看Array.prototype.find

翻译自: https://scotch.io/tutorials/quick-and-simple-search-filter-using-vanilla-javascript

vanilla

vanilla_使用Vanilla JavaScript的快速简单的搜索过滤器相关推荐

  1. JavaScript正则表达式快速简单的指南

    Interested in learning JavaScript? Get my ebook at jshandbook.com 有兴趣学习JavaScript吗? 在jshandbook.com上 ...

  2. vanilla_使用Vanilla JavaScript即时搜索

    vanilla Originally posted on www.florin-pop.com 最初发布在www.florin-pop.com The theme for week #15 of th ...

  3. vanilla_包装Vanilla JavaScript软件包以在React中使用

    vanilla Complex web projects often require the use of 3rd party widgets. But what if you're using a ...

  4. vanilla_使用Vanilla JavaScript构建Cookie库

    vanilla If you're like me, you are always on the lookout for jQuery libraries to use, and there are ...

  5. 使用 Vanilla JavaScript 框架创建一个简单的天气应用

    大家好,不知道大家听说过 Vanilla JavaScript 这款框架吗?最近我在浏览国外的一些技术网站时,这个词出现的频率实在是太高了,好多框架都宣称自己是基于 Vanilla JavaScrip ...

  6. vanilla_如何在Vanilla JavaScript中操作DOM

    vanilla by carlos da costa 通过卡洛斯·达·科斯塔 如何在Vanilla JavaScript中操作DOM (How to manipulate the DOM in Van ...

  7. vanilla_如何使用Vanilla JavaScript构建钢琴键盘

    vanilla Making a playable piano keyboard can be a great way to learn a programming language (besides ...

  8. Vanilla JavaScript 哈希 URL 路由器

    我看到一篇关于如何在纯 Vanilla JavaScript 中创建路由器的帖子.由于它不是在讨论散列路由,因此我决定创建这篇文章来与您分享我的知识. 我为什么要这么做? 多亏了 History AP ...

  9. javascript 代码_如何使您JavaScript代码保持简单并提高其可读性

    javascript 代码 by Leonardo Lima 莱昂纳多·利马(Leonardo Lima) 如何使您JavaScript代码保持简单并提高其可读性 (How to keep your ...

最新文章

  1. java 二进制图片上传_Spring MVC上传图片,Java二进制图片写入数据库,生成略缩图...
  2. (五)ElasticSearch 6.1.1数据类型
  3. kafka 在 360 商业化的实践
  4. 树莓派3ftp服务器修改地址,树莓派3搭建ftp服务器
  5. 关联分析(一)--Apriori算法
  6. 【matlab】拉普拉斯变换与反变换
  7. Linux宝塔禁止国外ip访问服务器,屏蔽国外ip访问网站代码(亲测有效)
  8. 暗黑版微信,官方发布! (附下载链接)
  9. UWB定位系统上位机源码
  10. java scene_JavaFX中场景(Scene)的意义是什么?
  11. 邓_phpcms_二次开发_留言板
  12. http协议get请求方法和post请求方法的区别
  13. 翻转课堂融入计算机课,【计算机基础论文】大学计算机基础翻转课堂的构建思路(共3745字)...
  14. 微信接口调用权限报: 48001, api unauthorized hint
  15. linux运维实习报告,it运维实习报告.docx
  16. python 入门到实践期末考试常出现的考试内容_测试类——python编程从入门到实践...
  17. 崔莺莺到宋楚瑜,张你怎么看待?
  18. cad vba 打开文件对话框_AutoCAD VBA选择文件夹的代码
  19. Pomelo Heartbeat
  20. uci拒绝认证_即将UCI认证!碟刹公路车的优势是什么?

热门文章

  1. 微信支付通用支付接口
  2. 磁盘(含优盘识别)读写速度测试
  3. txt文件转csv文件乱码问题
  4. 【Jenkins】Linux环境Jenkins下载与安装
  5. SoapUI-一款强大的Rest和Soap测试工具
  6. poky: qmmp_0.5.2.bb的问题以及修改方法。
  7. mysql数据库BKA算法详解
  8. JQuery CDN大全
  9. 第四章:Android灯光系统(6)-背光灯
  10. 华为网络工程师项目模拟