Test object for filtered results in VueJS or Vanilla JS with secondary search functionality

I've been tackling an intriguing challenge that has left me scratching my head. The code I have here is my latest attempt, and while it's close to working correctly, there seems to be something missing. Any assistance would be greatly appreciated. (This project is a volunteer endeavor for an animal adoption agency)

Here's the scenario:

I'm working with a Firebase database containing around 7,000 adopters. They are categorized into an Object structure like this:

{
  // Data fields of adopters 
}

Now, I have a search box set up like this:

<b-form-input id="searchInputString" v-model="searchInputString" name="searchInputString" type="text" placeholder="Search Adopters" class="form-control mb-3" autocomplete="off" @keyup.native="filterAdopters" />

The @keyup event triggers this function:

filterAdopters: function(event) {
  // JavaScript function logic
},

Simply put, I want to filter out results based on multiple search keywords entered by the user in a search query. The current method isn't yielding the desired results, am I missing something? Any suggestions on how to improve this approach?

Answer №1

To effectively filter data, it is recommended to assign all rows to a variable named results. Then, within a for loop, continue filtering the results using the following code snippet:

results = results.filter(s => {....

This process will progressively narrow down the matching rows based on each word in the search input.

While the provided solution has not been tested extensively, it is anticipated to function correctly:

filterAdopters: function(event) {
  this.spiltFilteredData = []

  if (!this.isNullOrEmpty(this.searchInputString)) {
    let searchArray = this.searchInputString.split(" ")
    console.log('Search Array =>', searchArray)

    // Retrieve all rows
    let results = this.allAdoptersData.map(s => {
      // Concatenate and lowercase all properties to optimize the filtering process
      // Include a new helper property, `concatenatedProps`, in all rows which will be removed later
      s.concatenatedProps  = [
        s.ApplicantCellPhone,
        s.ApplicantCity,
        s.ApplicantDLNum,
        s.ApplicantEmail,
        s.ApplicantEmployerName,
        s.ApplicantFirstName,
        s.ApplicantHomePhone,
        s.ApplicantLastName,
        s.ApplicantStreet,
        s.ApplicantZip,
        s.CoapplicantCellPhone,
        s.CoapplicantCity,
        s.CoapplicantDLNum,
        s.CoapplicantEmail,
        s.CoapplicantEmployerName,
        s.CoapplicantFirstName,
        s.CoapplicantHomePhone,
        s.CoapplicantLastName,
        s.CoapplicantStreet,
        s.CoapplicantZip
      ].join(' ') // Utilize ' ' for text joining as no search is conducted for spaces
       .toLowerCase()

      return s
    });

    for (let i = 0; i < searchArray.length; i++) {
      let searchText = searchArray[i].toLowerCase();

      results = results.filter( s => s.concatenatedProps.indexOf( searchText) !== -1 )
    }

    this.spiltFilteredData = results.map(s => {
      // Remove the helper property before returning the filtered data
      delete s.concatenatedProps

      return s
    })

  } else {
    this.searchInputString = ''
    this.spiltFilteredData = []
  }

  console.log('spiltFilteredData', this.spiltFilteredData)
},

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

What is the best way to retrieve ember model relation properties within routes and controllers?

Currently using ember 2.7.0, I am facing an issue while setting up my ember app with a currentUser.organization derived from the authenticated token. Although I can successfully resolve the currentUser, I am encountering difficulties in resolving the prope ...

The concept of expect-respond fails to function effectively when conducting unit tests in AngularJS with Jasmine

My goal is to populate the html with data using the pre-set response from the AngularJS mocking service (jasmine). In case the mock response fails, there is a fallback option where "http" would be displayed instead of "mock". However, the response always s ...

How can the @blur event be added to the vue-bootstrap-typeahead in Nuxt/Vue application?

I am facing an issue where I want to trigger a function upon leaving an input field. The input in question is set up using vue-bootstrap-typeahead. Upon inspecting the DOM, I found that the structure of the input element looks like this: <div id="m ...

Obtaining a file from the Firebase database

After successfully implementing the login functionality in my app, I am now attempting to retrieve the user's document in order to display their name on the page. email = loginemail.value; password = loginpassword.value; signInWithEmailAndPass ...

VueJS Element Library's date picker feature returns dateTime values with their corresponding time zones attached

I'm currently utilizing a date picker from The expected format for the date should be: yyyy-MM-dd However, the actual returned date format is: Thu Apr 20 2017 00:00:00 GMT+0530 (India Standard Time) This is the code snippet I've used: & ...

Looking for guidance on implementing throttle with the .hover() function in jQuery?

Seeking a way to efficiently throttle a hover function using jQuery. Despite various examples, none seem to work as intended. The use of $.throttle doesn't throw errors but ends up halting the animation completely. Here is the code snippet in question ...

What is preventing the installation of this Chrome extension?

I am struggling to install a few short and simple extensions on Chrome. The error message I receive about the 'manifest version' indicates that the issue might be due to outdated information. Since I lack experience in Chrome extensions and JavaS ...

What is the best way to assign IDs in JavaScript?

As I delved into learning about Objects in Javascript, I came across an example involving users that left me with a question. let users = [ { "id": "1", "name": "John", "age": 20 ...

Tips for implementing conditional styling (using else if) in a react component

Currently, while iterating through a list of header names, I am attempting to change the CSS style of a react component based on three different conditions. I have managed to make it work for one condition, but I am facing challenges when trying to impleme ...

What is the best way to save functions that can be utilized in both Vue front-end and Node back-end environments at the same time?

As I dive into the world of Node/Express back-end and Vue.js front-end development, along with server-side rendering, I am faced with the need to create utility functions that can format textual strings. These functions need to be accessible and reusable b ...

The sequence of Aurelia bindings processing

Is the order in which bindings on a page are processed consistent? In this example, binding behavior is applied before the custom attribute, even though it appears earlier in the markup. Check out this Gist for reference. <form my-attribute.bind="att ...

In version 81 of Three.js, the toShapes() function has been relocated to a new class. Find out how

After updating Three.js from v73 to v81, I encountered the following error: Uncaught TypeError: path.toShapes is not a function Upon reviewing the new release notes, I discovered the following changes made to Path: The removal of .actions (yay) Movin ...

Is it more effective to import an entire library or specific component when incorporating it into Create-React-App?

I have a question about optimizing performance. I understand that every library has its own export method, but for instance, on react-bootstrap's official documentation, it suggests: It is recommended to import individual components like: react-boo ...

Leveraging the power of Firebase and JavaScript to easily include custom user fields during the user

UPDATE: I encountered an issue while using Nextjs framework. However, it works perfectly fine when I use a vanilla CRA (Create React App). It appears that the problem is somehow related to Nextjs. I'm currently working on creating a new user document ...

Deciphering the outcome of filtering tasks

How can I use filters to swap a returned value into an HTML checklist icon like this? filters: { checkStatus: function(value){ if(value > 0){ return '<span class="bg-orange-400 text-highlight"><i class= ...

Is it possible to continuously generate webpages using AJAX?

Is there a way to achieve an infinite scrolling effect in all directions using ajax requests without the need for flash or silverlight? If anyone has an example of this, I would love to see it! Thank you for your time. ...

Elegant transition effects for revealing and hiding content on hover

While customizing my WordPress theme, I discovered a unique feature on Mashable's website where the social buttons hide and show upon mouse hover. I'd love to implement this on my own site - any tips on how to achieve this effect? If you have ex ...

Does the data in a Vuex store always stay in memory?

Firstly, I want to thank you for your patience with my limited English skills. I am curious if the data in Vuex's store always stays in memory. Let me provide an example. If we receive a list from the server and store it in the Vuex state when enteri ...

Running a Node.js script on an Express.js server using an HTML button

I've got an HTML button: <button id="save" type="button" onclick="save()">Save</button> and in my JavaScript code, I want it to execute something on the server side using node.js Here's what I have in mind: function save() { / ...

Implementing a one-time watcher with user input in Vue.js

I am facing an issue with using the input tag in a Vue template. I need to change the type from 'password' to 'text'. <input type="text" v-model="form.password" /> To achieve this, I have created a watch code to convert text s ...