Vue allows you to use regular expressions with arrays

Looking to implement a list filtering system using checkboxes.

This is how I am looping through an array from VUEX:

<div class="checkbox" v-for="brand in brands" :key="brand.id">
   <input name="brands" type="checkbox" :value="brand.name" v-model="checkedBrand" />
   <label for="brands">{{brand.name}}</label>
</div>

Here is the function I am using:

filteredList() {
      if (this.checkedBrand.length > 0) {
        return this.shoes.filter(shoe => {
          return shoe.brand.match(
            new RegExp(
              this.checkedBrand.forEach(check => {
                return +check + "|";
              }),
              "g"
            )
          );
        });
      } else {
        return this.shoes;
      }
    }

I currently have the line: When it's new RegExp(checkedBrand[0]+'|'+checkedBrand[1], 'g'), but I want to avoid hardcoding that.

Answer №1

selectedItems() {
  if (this.selectedBrands.length > 0) {
    return this.items.filter(item => {
      return item.brand.match(
        new RegExp(
          this.selectedBrands.join('|'),
          "g"
        )
      );
    });
  } else {
    return this.items;
  }
}

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

When text is wrapped within the <li> tag

In this div, the structure is as follows: <div class="box1" id="infobox"> <h2> Point characteristics: </h2> <div style="padding-left:30px" align="left"> <ul> <li class="infobox_list"><b>X value: </b>< ...

Initiating an AJAX request to communicate with a Node.js server

Recently, I started exploring NodeJS and decided to utilize the npm spotcrime package for retrieving crime data based on user input location through an ajax call triggered by a button click. The npm documentation provides the following code snippet for usi ...

I developed an RPG game with an interactive element using jQuery. One of the biggest challenges I'm facing is the random selection process for determining which hero will be targeted by the enemy bots during battles

Hello, this marks my debut on stack overflow with a question. I've created a game inspired by old school RPGs where players choose from three heroes based on the Marvel universe to battle one of three enemies. The problem I'm facing is that even ...

Compile a roster of service providers specializing in unit testing imports

Recently joining a new team that works with Angular, they asked me to implement unit testing on an existing project built with Angular 8. After researching the best approach, I decided to use Karma + Jasmine for testing. I set up a .spect.ts file structure ...

Enhancing gallery user experience with jquery to animate the opacity of active (hovered) thumbnails

I am attempting to create an animation that changes the opacity of thumbnails. By default, all thumbnails have an opacity of 0.8. When hovered over, the opacity should increase to 1 and then return to 0.8 when another thumbnail is hovered over. Here is th ...

Angular alert: The configuration object used to initialize Webpack does not conform to the API schema

Recently encountered an error with angular 7 that started popping up today. Unsure of what's causing it. Attempted to update, remove, and reinstall all packages but still unable to resolve the issue. Error: Invalid configuration object. Webpack ini ...

What is the best method for validating multiple fields in a form with Javascript?

I am currently working on validating multiple fields within a form. Although I lack experience in JavaScript, I have managed to piece together a code that is functional for the most part. If all fields are left blank, error messages prompt correctly. Howe ...

Looking to personalize the MUI - datatable's toolbar and place the pagination at the top?

I successfully managed to hide the toolbar icon, but I am struggling with positioning pagination from bottom to top. Additionally, I am attempting to add two buttons (reset and apply) in the view-Column toolbar without any success in customizing the class. ...

Updating a marker in real-time using API response

I'm trying to create a simple web application that allows users to enter a city name in an input box, which then triggers the updateMap function to geolocate and map the city with a marker. After mapping the city, another function called updateTemp is ...

Incorporate personalized feedback into a datatable with server-side processing

Trying to implement server-side processing for a datatable loaded from an AJAX response using this specific example The server response being received is as follows: {"msg":null,"code":null,"status":null,"result":[{"aNumber":"3224193861","bNumber":"32159 ...

Instructions for concealing and revealing a label and its corresponding field before and after making a choice from a dropdown menu

Currently, I am working on developing a form that will enable customers to input their order information. This form includes a selection list for payment methods, with three available options. If the user chooses either credit or debit card as the paymen ...

How can you proactively rebuild or update a particular page before the scheduled ISR time interval in Next.js?

When using NextJS in production mode with Incremental Static Regeneration, I have set an auto revalidate interval of 604800 seconds (7 days). However, there may be a need to update a specific page before that time limit has passed. Is there a way to rebui ...

Adjust the value based on selection

I aim to display another div when either the Full Time or Part Time option is selected. Additionally, I would like to calculate a different value for each option; if 'Part Time' is chosen, PrcA should change to PrcB. Here is the code snippet tha ...

What sets srcset apart from media queries?

Can you explain the contrast between srcset and media query? In your opinion, which is more optimal and what scenarios are ideal for each? Appreciate it! ...

Need help with a countdown function that seems to be stuck in a loop after 12 seconds. Any

I am facing an issue with a PHP page that contains a lot of data and functions, causing it to take around 12 seconds to load whenever I navigate to that specific page. To alert the user about the loading time, I added the following code snippet. However, ...

Tips for implementing an onClick event within a NavBar component in a React application

Embarking on my first React project has been an exciting journey for me. I am relatively new to JavaScript, HTML, CSS, and the world of web app programming. My goal is to showcase some information upon clicking a button. In my main default component, App ...

Tips for implementing server-side pagination with Angular-UI Bootstrap by utilizing the skip parameter

i have a number of items generated using ng-repeat, i have to implement pagination.but everywhere i can see by getting the count value of items, i can add. but i am struggling with implementing skip functionality. For example, if the skip value is set to 1 ...

Retrieving environmental information within a Vue component

I am trying to display information from my .env file, specifically the APP_NAME, in my components. For example, I want to greet users with Welcome to {{APP_NAME}}. UPDATE After referring to this documentation, I have updated my env file as follows: MIX ...

The removeEventListener function is failing to properly remove the keydown event

Every time my component is rendered, I attach an event listener. mounted() { window.addEventListener("keydown", e => this.moveIndex(e)); } Interestingly, even when placed within the moveIndex method itself, the event listener persists and cannot ...

What could be causing my Vue application to not launch after executing `npm run serve`?

These past 24 hours have been a struggle for me. I recently embarked on the journey of learning Javascript, and my choice of JS framework was Vue JS. However, when I run npm run serve, my Vue JS app bombards me with numerous errors that seem to make no se ...