1. Project ProspectsAs Vue and React are more and more widely used in actual development, the front-end processes more and more data. The main purpose of this article is to process the data returned by the server and perform fuzzy queries according to the conditions, so as to reduce the number of requests sent to the server and improve performance and user experience. The following is a simple DEMO to implement the fuzzy query function: The test data is as follows: var data = [{ "title": "How long does it take for you to recover from a broken heart?", "title_image": "https://img.doutuimao.net/dtmimg/b7c9ec393414982682e5a477eb995b55", "desc": "Food and Beverage Test How Long It Takes to Recover from a Broken Heart", "id": "2", "num": 951357, "flag": "01" }, { "title": "How high is your coquettishness index?", "title_image": "https://img.doutuimao.net/dtmimg/60d8ed86d1f72357c194506270c72ac1", "desc": "Women who can act coquettishly are the most fortunate. How good are you at acting coquettishly? How charming are you when you act coquettishly? Come and take the test!", "id": "3", "num": 963258, "flag": "01" }, { "title": "How will you get married in the future?", "title_image": "https://img.doutuimao.net/dtmimg/538632e75159ce8e586778d289c66a11", "desc": "I believe many people are looking forward to their future wedding methods. Do you want to know how you will get married in the future?", "id": "4", "num": 879564, "flag": "01" }, ] 2. Knowledge PointsObject.assign() usageThe Object.assign method is used to copy all enumerable properties of the source object (source) to the target object (target). It requires at least two objects as parameters, the first parameter is the target object, and the subsequent parameters are all source objects. // Processing original array let arrnew = data.map((item, index) => { return Object.assign({}, { 'desc': item.desc, }) }) filter() MethodThe filter() method is used to filter array elements. The method creates a new array containing all elements that pass the test implemented by the provided function. filter() does not detect empty arrays and does not mutate the original array. indexOf() fuzzy queryThe indexOf() method returns the position of the first occurrence of a specified string value within a string. If the string value to be retrieved does not appear, the method returns -1. In conjunction with the filter() method, it can detect whether the array contains the input value and return it. var newData = arrnew.filter(item => { if (item.desc.indexOf(value) > -1) { //In the indexOf method, if xxx.indexOf("") returns a value of 0 return item } return newData }) The complete DEMO code is as follows:<div class="wrap"> <input type="text" id="demo"> <ul id="newsBox"> </ul> </div> function create() { var value = input.value; var html = ""; let arrnew = data.map((item, index) => { return Object.assign({}, { 'desc': item.desc, }) }) var newData = arrnew.filter(item => { if (item.desc.indexOf(value) > -1) { //In the indexOf method, if xxx.indexOf("") returns a value of 0 return item } return newData }) if (newData.length > 0) { for (var i = 0; i < newData.length; i++) { html += `<li>${newData[i].desc}</li>` } } else { html += `<li>No data</li>` } ul.innerHTML = html; } creat() input.onchange = function(e) { creat() } SummarizeThis is the end of this article about implementing local fuzzy search function in JavaScript. For more relevant JS local fuzzy search content, please search previous articles on 123WORDPRESS.COM or continue to browse the related articles below. I hope everyone will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Simply learn various SQL joins
>>: Detailed explanation of the use of Linux lseek function
We often see a cool effect where the mouse hovers...
Table of contents 1. Check whether the docker env...
This article shares the implementation method of ...
Table of contents Preface 1.v-show 2.v-if 3. The ...
1. Add an empty element of the same type, and the...
To facilitate the maintenance of MySQL, a script ...
Preface Because of project needs, the storage fie...
1. Download related tools and images Download Lin...
There are two most commonly used methods to insert...
Preface When we write code, we occasionally encou...
Table of contents System update configuration Cha...
Preface I am a PHP programmer who started out as ...
Preface ORDER BY 字段名升序/降序, I believe that everyon...
Similar to the code hosting service provided by G...
There is no need to say much about the difference...