Example of how to implement local fuzzy search function in front-end JavaScript

Example of how to implement local fuzzy search function in front-end JavaScript

1. Project Prospects

As 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 Points

Object.assign() usage

The 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() Method

The 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 query

The 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()
}

Summarize

This 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:
  • JavaScript to implement front-end paging function
  • JavaScript front-end implements image compression function
  • JS implements the search function of the front-end page
  • Share 10 common JavaScript front-end handwriting functions

<<:  Simply learn various SQL joins

>>:  Detailed explanation of the use of Linux lseek function

Recommend

Example code for implementing dotted border scrolling effect with CSS

We often see a cool effect where the mouse hovers...

Detailed steps for installing MinIO on Docker

Table of contents 1. Check whether the docker env...

Native JavaScript carousel implementation method

This article shares the implementation method of ...

Several methods to clear floating (recommended)

1. Add an empty element of the same type, and the...

Detailed explanation of how to use grep to obtain MySQL error log information

To facilitate the maintenance of MySQL, a script ...

Basic JSON Operation Guide in MySQL 5.7

Preface Because of project needs, the storage fie...

How to insert video into HTML and make it compatible with all browsers

There are two most commonly used methods to insert...

JS implements the sample code of decimal conversion to hexadecimal

Preface When we write code, we occasionally encou...

Windows 2016 Server Security Settings

Table of contents System update configuration Cha...

MYSQL transaction tutorial Yii2.0 merchant withdrawal function

Preface I am a PHP programmer who started out as ...

Some lesser-known sorting methods in MySQL

Preface ORDER BY 字段名升序/降序, I believe that everyon...

Analysis of the operating principle and implementation process of Docker Hub

Similar to the code hosting service provided by G...

Detailed tutorial on building Gitlab server on CentOS8.1

There is no need to say much about the difference...