Apply various filters to extract and refine information from the database

I have successfully retrieved data from the database.

The structure of the data is as follows:

serie
   --- title (string)
   --- category (array)

To filter the data, I have implemented a search filter using a computed property. This is how it looks:

filteredSeries () {
  return this.series.filter(serie => {
    return serie.title.toLowerCase().match(this.search.toLowerCase())
  })
}

Looping through the series is done in the following manner:

<v-flex xs12 sm4 md3 lg2 v-for="serie in filteredSeries" :key="serie.title" pa-3>

.....

</v-flex>

The search term is obtained from here:

<v-text-field label="Search" height="35" v-model="search" prepend-inner-icon="search"></v-text-field>

Everything is working smoothly so far, but now I'm facing a challenge. I want to filter the series not only by the title but also by category.

All categories are obtained from the data method and stored in an array like this:

data () {
  return {
    series: [],
    search: '',
    categories: [
      'Crime', 'Drama', 'Mistery', 'Comedy', 'Horror', 'Sci-Fi'
    ],
    filterCategory: []
  }
},

The filter select dropdown is populated with data like this:

<v-select  prepend-inner-icon="category" height="35" v-model="filterCategory" :items="categories" chips label="Category" multiple></v-select>

This select dropdown returns a filterCategory array. My goal is to fetch series based on specific categories selected in filterCategory. I am unsure of how to achieve this. Ideally, filtering by category should be incorporated into the computed property where search filtering is currently being performed.

If anyone has any suggestions or solutions on how to accomplish this, I would greatly appreciate it.

Answer №1

To improve your filter method, you can now include the option to filter by filteredCategories.

This can be achieved using JavaScript's Array.every() and Array.includes() methods.

Additionally, it is suggested to use String.includes() instead of String.match(), as match() is typically used for regular expression searches.

filteredSeries () {

  // Filtering based on selected categories.
  const filteredSeries = this.series.filter(serie => {
    return this.filteredCategories.every(category => {
      return serie.categories.includes(category);
    });
  });

  // Further filtering by search term.
  return filteredSeries.filter(serie => {
      return serie.title.toLowerCase().includes(this.search.toLowerCase()));
  });
}

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

Display a specific section of an image as the background in a div, with the image scaled and positioned perfectly

Utilizing a 1900 x 1080 image within a div as the background <!DOCTYPE html> <html> <head> <title></title> <meta charset="utf-8" /> <style> html,body { height:100%; } #imageHolder{ ...

Angular 7 error: No provider found for PagerService causing NullInjectorError

I have been struggling to get pagination working properly in my project. Below is the code I have written: pager.service.ts: import * as _ from 'underscore'; @Injectable({ providedIn: 'root', }) export class PagerService { ...

Using a v-progress-circular within a conditional statement is causing the elements below to appear "jerky"

The circular progress indicator will continue to display until the condition sending becomes false. However, when the div disappears, the element below it suddenly jumps up, creating a "jumpy" effect. What is the most effective way to present this to ens ...

Configuring the data source for an autocomplete feature in ReactJS Material UI

For one of my React components, I am utilizing the Material UI autocomplete feature. The data source is retrieved from the server asynchronously and has the following structure: const dataSource = [{ id: 001 firstName: 'fname', lastName: &apo ...

Encountering errors while testing a NUXT.js and Vue.js application using Jest, specifically seeing messages like '[vuex] module namespace not found in mapState()' and '[vuex] unknown action type'

Despite NUXT's automatic namespacing feature, I am encountering difficulties testing or referencing the store in my testing modules. Can anyone offer advice on how to edit the namespacing property in a Nuxt app? Below is the code for the component, s ...

specific css styles only for Safari and Internet Explorer

Imagine a scenario where I have a div with the class 'x': <div class = 'x' /> This div has some CSS properties and what's interesting is that it appears differently in Safari and the latest version of IE compared to other ...

What is the best way to insert objects into another array of objects at alternating positions?

I'm currently developing a React component and have a requirement to insert a new object at alternate positions within an array of objects. For example: arr1 = [{test: 1},{test: 2},{test: 3},{test: 4}] The expected output should look like this: arr ...

Leveraging JavaScript Functionality with ASP.NET Identity Roles

I'm currently working on an application that utilizes JQuery DataTables. The goal is to have these tables visible to all users, but restrict the click functionality to a specific user role. One way I can achieve this is by setting up authorization on ...

Shrink Font-Awesome icon sizes using a Node.js and Express.js web application

Currently, I am in the process of developing a website using node.js + express.js and implementing font-awesome for icons. The local hosting of Font-awesome has resulted in a 1.2 MB JS file [font-awesome.js], even though we are only utilizing 10-15 icons. ...

Does anyone know if there is a way to use JavaScript to implement LESS in CSS files

Is there a way to perform variable substitution, especially in CSS using JavaScript? I am looking for a solution that offers more features than just variable substitution, such as the LESS syntax. Has anyone come across a JavaScript implementation of LES ...

Using the information retrieved from Google Place Autocomplete and saving it for future reference

I am interested in utilizing the Google API "Place Autocomplete" widget to enhance user experience on my website. The specific widget I have in mind can be found here. My goal is to streamline the process of obtaining property addresses from Real Estate A ...

The Google Drive API in Node.js is notifying the deletion of files

I encountered an issue with the Google Drive API in my application. Even after deleting files from Google Drive, the listfiles function still returns those deleted files. Is there a solution to prevent this from happening? Below is the function of my API: ...

How can you use the MongoDB Aggregation Framework to filter by a range of dates, group results by individual days, and calculate the average value for each day

I'm currently exploring the possibilities of MongoDB's Aggregation Framework and would appreciate some assistance in enhancing this query to achieve the following objectives: Retrieve Records with Dates falling within a specified range Organize ...

Is there a better approach to verifying an error code in a `Response` body without relying on `clone()` in a Cloudflare proxy worker?

I am currently implementing a similar process in a Cloudflare worker const response = await fetch(...); const json = await response.clone().json<any>(); if (json.errorCode) { console.log(json.errorCode, json.message); return new Response('An ...

Preserving Previous Values in Vuetify Select

Experience a unique way of handling Vueitfy selections with this code snippet: <v-select label="..." autocomplete append-icon="search" :items="plots" item-value="id" item-text="plotHeader" v-model="selectedPlot" v-on:change="loadPlotInforma ...

What is the best way to iterate through an ID using jQuery?

After pulling the list of doctors in my area from the database and displaying it on my webpage, I now want to load each doctor's "About" content inside a Bootstrap modal. I added an "about" column within the same database table where the doctors' ...

JavaScript implementation of a carousel slider with responsive design

I have successfully implemented a feature carousel slider using the jquery.featured.carousel.js file along with some CSS. Here is the jsfiddle link to my project: LINK. When running this code on localhost, I noticed that I need to refresh the page every ...

Is it possible to generate a triangular attachment below a div element?

My designer sent me a unique design and I'm wondering if it's possible to replicate using HTML, CSS, or JavaScript? https://i.stack.imgur.com/spB71.png I believe it can be done with CSS by creating a separate div positioned absolutely under the ...

Encountering Issues with NextJS Dynamic SSR: Mobile Devices stuck on loading screen

Issue: The dynamic import feature of Next JS is encountering loading issues specifically on mobile browsers such as Google Chrome and Safari on IOS. Strangely, the functionality works smoothly on desktop browsers like Google Chrome and Mozilla. The projec ...

The positioning of the menu icons varies

When it comes to displaying the search icon in the WordPress toggle bar, I use a custom JavaScript file. This setup is mainly focused on website design and menu functionality. Additionally, I have integrated a newsletter subscription plugin for managing su ...