Utilizing Vuejs to effectively apply filters on object properties

Currently, I am working with two data sets: categories and products. My goal is to filter products based on the category they are associated with.

The challenge lies in filtering by category.name as the name must match product.category. While attempting to pass this on, I have tried utilizing category["name"] and {{category.name}} without success.

<b-tab v-for="category in categories" v-bind:title="category.name" :key="category.id">
<div v-for="product in filteredByCategory(category.name)" :key="product.id" class="slide">
  <b-card
    v-bind:title="product.name"
    :img-src="product.imageUrl"
    style="min-width: 15rem; max-width: 15rem;"
   >
</b-card>

The current method I am using for filtering:

filteredByCategory(category){
      return this.products.filter(product => product.category === category.name)
    }

If anyone has a solution or suggestion, please assist me.

Answer №1

The name has already been passed here:

filteredByCategory(category.name)

However, attempting to access the name property again in the filter function.

product.category === category.name

You can make one of the following changes:

  1. Pass the category object instead - filteredByCategory(category)
  2. Or check against the category itself - product.category === category

Answer №2

In ES6, dot notation is actually accepted.

function({ propHere })

I noticed that you are passing the category.name to the function filteredByCategory, but then using category.name. This results in something like 'clothing'.name which is undefined because the string prototype does not have such a method.

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

Troubleshooting three.js problem: Unexpected application malfunction in Chrome due to outdated code incompatible with the latest three.js library

Several weeks ago, my three.js (R48) applications were running smoothly until I encountered issues with Chrome where they no longer work. Here are the initial error messages I received: WebGL: INVALID_OPERATION: getAttribLocation: program not linked skyW ...

How do I link the ID div to a Vue button registration?

I have a specific setup like this: <div v-show="visible" id="ten"></div> <div v-show="visible" id="twelve"></div> <button v-if="!isHidden" v-on:click="visible=!visible, isHidden = true">Click Me!</button> <script&g ...

Use jQuery in MVC 5 to remove each cloned div individually starting from the last one, excluding the default div

My default setting is as shown below. https://i.stack.imgur.com/m8Hpr.png The add button functions correctly, but I am having trouble removing cloned divs. I can only remove the last one, instead of each cloned div individually starting from the last, exc ...

efficiency of this algorithm

function checkSubset(array1, array2) { const set1 = new Set(array1); const set2 = new Set(array2); for (const element of set2) { if (!set1.has(element)) { return false; } } return true; } Can we consid ...

What is the correct method to choose an element in jQuery based on the function's id parameter

I'm having trouble deleting an item in my to-do list app. deleteToDoItem: function(id) { $(id).remove(); console.log(id); }, Here is the function that calls deleteToDoItem: function deleteItem(event) { var itemID, splitID, t ...

Perform individual conditions (filters) sequentially within a MongoDB query

Currently, I am working on a query that involves using $and to apply multiple filters or conditions. While explaining the query, I realized that $and does not function in the same way as the JavaScript && operator. How can I simulate JavaScript && functi ...

Issue with IPv6 compatibility in Node.js and Selenium causing SocketException due to unavailable protocol family

Encountering this error appears to be unique to when the ios-driver jar is spawned as a Node.js child process. The specific error message is java.net.SocketException: Protocol family unavailable selenium-test.js: var spawn = require('child_process& ...

Issue with dependency injection arises when module definition is used

As a beginner in Angular, I'd greatly appreciate it if you could point out any rookie mistakes I may be making here. Below is my module and controller structure: (function () { 'use strict'; angular .module('myApp&ap ...

Is it common for all my components to become class components when I integrate Redux into ReactJS?

During my experience with learning react, my instructor consistently emphasized the importance of using functional components whenever possible, and advised to limit the use of class components. It seemed straightforward at the time. However, now that I a ...

In JavaScript, when a div element contains an iframe, the click event will not trigger on the div

Check out this sample HTML markup: <div> <iframe></iframe> </div> The iframe is set to fill the outer div. I've added an event handler to the div click event: div.addEventListener("click", function () { console.log( ...

Failure to display alert message upon completion of AJAX request

I am attempting to use AJAX to send data to the database without refreshing the page when a user favorites a message. Even though the data is successfully sent to the DB, the page still reloads and the alert message I want to display is not showing up. Th ...

Remove a picture from Cloudinary by utilizing the axios delete method

I am struggling to find a solution for deleting images on Cloudinary using axios delete request. It seems there is no specific API URL or code snippet available that can help me achieve this. Here is the code I am using to upload an image on Cloudinary: ...

What is the best way to customize the functionality of the Done (node-dialog-ok) button in node-red when it is

I have created a custom node and I want to trigger some specific actions when the 'Done' button (with id: node-dialog-ok) in the editor-tray-toolbar is clicked, instead of its default functionality. Is it possible to override the onclick event o ...

Implementing multiple filters with jQuery

Make a Selection `<select class="form-control" id="technology"> <option name="sort" value="2g" id="2g"gt;2G</option> <option name="sort" value="3g" id="3g"&g ...

"Combining the power of Node.js, mysql, and socket.io

As a beginner with socket.io, I am facing an issue. How can I update and display real-time data from a table in my database without having to restart the node.js server? The table receives new data every 10 seconds. I have followed several tutorials but h ...

Code to dynamically add a div to an HTML template using JavaScript logic

A simple web application was developed by me to retrieve recipe data from an API. This data is then displayed by inserting it into an HTML template specified in the JavaScript file. The layout is managed using a float grid in CSS. Here is the code snippet ...

Creating an XML response to generate an HTML dropdown menu in PHP

My onChange function in javascript calls a PHP file to fetch UPS rates and update an HTML dropdown list. Everything was working fine until I needed to add an item to the options list based on a comparison. Javascript: function fetch_UPS(el){ var zip = ...

Attempting to use express and nodemailer to send an email, but there is absolutely no output appearing in either the console or the terminal

When I click the send email button, it should send the data to a mailhog inbox. However, nothing happens - no errors in the console or terminal. My goal is to use nodemailer to send the name, email, chosen plan, and message from the form to the mailhog add ...

Angular with Firebase: How to ignore a field in a query

I am curious to see if my current structure is compatible with Firebase, or if I need to make adjustments. Let's take a look at an example using the "/rooms" endpoint, which contains an array of Room objects: export class Room { id: number; p ...

Angular - Displaying a notification when the timezone does not align with the current moment in time

I am currently working on an angular project where users are only allowed to create tasks for today or future dates using a date picker. I have implemented the <mat-datepicker> along with moment to disable past dates. <mat-form-field formGroupNa ...