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

Method for realizing Internet interconnection by VMware virtual machine bridging

After installing VMware and creating a new virtua...

Simple implementation method of Linux process monitoring and automatic restart

Purpose: Under Linux, the server program may be d...

SQL implementation of LeetCode (183. Customers who have never placed an order)

[LeetCode] 183.Customers Who Never Order Suppose ...

Some ways to eliminate duplicate rows in MySQL

SQL statement /* Some methods of eliminating dupl...

How to collect Nginx logs using Filebeat

Nginx logs can be used to analyze user address lo...

Implementation of importing and exporting vue-element-admin projects

vue-element-admin import component encapsulation ...

MySql index detailed introduction and correct use method

MySql index detailed introduction and correct use...

Simple usage of MySQL temporary tables

MySQL temporary tables are very useful when we ne...

Vue-Router installation process and principle detailed

Table of contents 1. Front-end routing implementa...

A brief discussion on the issue of element dragging and sorting in table

Recently, when using element table, I often encou...

How to skip errors in mysql master-slave replication

1. Traditional binlog master-slave replication, s...