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

MYSQL slow query and log settings and testing

1. Introduction By enabling the slow query log, M...

Writing a shell script in Ubuntu to start automatically at boot (recommended)

The purpose of writing scripts is to avoid having...

How to create WeChat games with CocosCreator

Table of contents 1. Download WeChat developer to...

Detailed explanation of the principle of creating tomcat in Eclipse

When creating a tomcat server on a local eclipse,...

HTML web page hyperlink tag

HTML web page hyperlink tag learning tutorial lin...

Example code of setting label style using CSS selector

CSS Selectors Setting style on the html tag can s...

Native js custom right-click menu

This article example shares the specific code of ...

MySQL database introduction: detailed explanation of database backup operation

Table of contents 1. Single database backup 2. Co...

MySQL 8.0.24 version installation and configuration method graphic tutorial

This article records the installation and configu...

jQuery implements ad display and hide animation

We often see ads appear after a few seconds and t...

React handwriting tab switching problem

Parent File import React, { useState } from '...