What is the process for developing a personalized search feature in VueJS?

How can I create a custom search function in VueJS that allows me to input partial product names and receive relevant results?

For example, I have 2 products named PlayStation Plus 90 Days IE and PlayStation Plus 90 Days NO. Currently, I have to input the full name to find a product, but I want to be able to input just PlayStation 90 and see both products in my search results. How can I achieve this?

I attempted to create a search function using the code snippet below, but it did not work as expected:

computed: {
customSearch() {
  return this.result.filter(product => {
    return product.text
      .toLowerCase()
      .includes(this.searchInput.toLowerCase())
  })
}

Additionally, I considered splitting the string into an array of keywords like

["PlayStation", "Plus", "90", "Days", "IE"]
and searching based on those keywords, but I am unsure how to implement this. Can someone provide guidance?

Thank you for your assistance!

Answer №1

Utilizing the Array#every method:

const 
  items = [{ text:"PlayStation Plus 90 Days IE" }, { text:"PlayStation Plus 90 Days NO" }],
  searchQuery = "PlayStation 90";

const searchTerms = searchQuery
  .toLowerCase() // convert to lowercase
  .split(/\s+/); // split into individual words
const outcome = items.filter(({ text }) => 
  searchTerms.every(term => // ensure every word is in the text of the current item
    text.toLowerCase().includes(term)
  )
);
  
console.log(outcome);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Issues with Google maps are causing multiple maps to malfunction

After incorporating some jquery code to create multiple maps upon window load, I noticed a peculiar issue with the maps - they all display the same location despite having different latitudes and longitudes set. Upon inspecting the code responsible for cr ...

Newbie's guide to setting up babel for material-ui in Next.js!

Helpful Resources: Click here "For better bundle size optimization, create a .babelrc.js file in your project's root directory: const plugins = [ [ 'babel-plugin-transform-imports', { '@material-ui/core': { ...

Firefox does not support jQuery AJAX functionality, unlike Internet Explorer where it works smoothly

Can anyone help me figure out how to ensure that this code works smoothly on both IE and Firefox? The confirm prompts are functioning in Firefox, but the AJAX request isn't triggering. According to Firebug, there's an error at line 9631 of jquery ...

Exploring Heroes in Angular 2: Retrieving Object Information by Clicking on <li> Items

Currently, I am delving into the documentation for an angular 4 project called "Tour of Heroes" which can be found at https://angular.io/docs/ts/latest/tutorial/toh-pt2.html. <li *ngFor="let hero of heroes" (click)="onSelect(hero)">{{hero.name}}< ...

Convert the numerical values from an array into an input field format

Currently, I have two inputs and an array with two number positions. The v-model in each input corresponds to a value in the array. Whenever a change is made in either input field, it reflects on the corresponding position in the array, which works perfect ...

The IDs and classes of HTML elements

I have two different implementations of a livechat script. On my sandbox site, the livechat is fixed to the bottom right of the page and scrolls with the window. However, on my live site (where this specific code comes from), it is attached to the footer. ...

Troubleshooting the issue of a callback function not properly updating state within the componentDidMount

I am currently utilizing Next.js and have the following functions implemented: componentDidMount = () => { //Retrieves cart from storage let self = this this.updateCart(Store.getCart(), self) ... } updateCart = (cart, self) => { ...

extracting the HTML content from JavaScript and saving it into a standalone file

update When I click a link, a popup opens and I see all this HTML. The smile method is called when I click the link, and we append HTML in that method so that we can see it when the popup is opened. I moved it to a separate file something.component.html, ...

Utilizing JavaScript recursion to navigate through a JSON object and update specific key-value pairs on its nested children

Exploring a JSON object structure that follows a tree-like child-parent relationship. Each node in the structure has a unique ID. Attempting to iterate through the entire object using a recursive function, encountering challenges with handling the children ...

Modify the code to interpret a new JSON structure

I have a piece of code that is designed to read JSON data: $.getJSON("data.json", function(data){ var output = ''; $.each(data, function(index, value){ output += '<li>' + value.title + '</li>'; } ...

VueJS - Identical keys found while iterating through md-table items

I am attempting to display a material table for a database object that repeats its IDs. <md-table v-model="data"> <md-table-toolbar> <div class="md-toolbar-section-start"> <h1 class="md-t ...

A JavaScript function that yields no value is undefined

In my AngularJS project, I have a function that makes a GET request to the backend multiple times and returns data. Here is an example of how the function is used: function getFunction(inputData) { $http.get('request URL', { params: 'so ...

Daily loop countdown timer

Looking to implement a daily countdown timer that ends at 10am, I have the following code set up: setInterval(function time(){ var d = new Date(); var hours = 09 - d.getHours(); var min = 60 - d.getMinutes(); if((min + '').length == 1){ ...

What are some methods to secure my API keys within my React application?

What steps can I take to secure my api keys in my react application? Should I incorporate something with express? My goal is to avoid creating any server-side components to handle the API calls. Currently, my backend is managed by firebase but I also uti ...

the mobile website is not properly aligned on a horizontal axis

As I work on creating a mobile version of my website, I've come across a challenge: The entire site fits perfectly on the computer at a browser width of 480px, but when viewed on my mobile phone (regardless of the browser used), it leaves space on the ...

Angular directive to delete the last character when a change is made via ngModel

I have 2 input fields where I enter a value and concatenate them into a new one. Here is the HTML code: <div class="form-group"> <label>{{l("FirstName")}}</label> <input #firstNameInput="ngMode ...

Obtaining a response in string format using the $.ajax function

var module = (function(){ return{ loadData: function(url, success, error){ $.when($.ajax({ type: 'GET', cache: false, url: url, contentType: 'application ...

When a base html tag is dynamically added, the browser mistakenly loads assets twice

When managing relative paths on a website, I utilize the <base> tag within the <head> section of each page. Although all resources loaded via relative-like paths in the documents are displayed correctly, my observations show that browsers such ...

I created some jQuery code that modifies a button when it is hovered over, however, I ended up writing the code individually for each button. What steps can I take to turn it

Is there a way to turn the code for each button on my website into a jQuery function that can be applied to any button? This is how the code currently appears: $(document).ready(function() { $("#linkXML").hover( function() { ...

The issue arises with the Google Map marker failing to function correctly when pulling data

I've recently started learning Google Maps. It's interesting that markers work and are displayed when statically declared, but not when retrieved from a database. // var markers = [[15.054419, 120.664785, 'Device1'], [15.048203, 120.69 ...