What is the best way to apply an "active" class to the images that are displayed depending on the object's properties?

Below is a link to the JSON file I am working with -

The JSON file consists of 5 main categories of runes, with each category containing 4 slots. Each slot has 3 runes, except for the first slot which has 4 runes. The code snippet below loops through the slots and displays images of all the runes in each slot. The final output appears as shown in this image:

https://i.sstatic.net/L8h3Y.png

However, I am facing an issue where I need to add an 'active' class to the runes that have been selected by the user. The selected runes are stored as properties within a data object named match.

data(){
    return {
        match: null
    }
},
methods: {
    primaryPerks(){
        let perksTree = Object.entries(this.$store.state.summonerRunes).find(([key,value]) => value.id === this.match.mainParticipant.stats.perkPrimaryStyle);

        return perksTree[1]
    }
}

The value of match is currently null as it is populated post a GET request. Within this object, there are properties named perk0, perk1, perk2, and perk3 which hold the IDs of the selected runes. I need to modify my iteration logic to add an 'active' class to the images of those runes whose IDs match any of the perk properties. However, I'm unsure about how to implement this check.

<div class="runes">
    <div v-for='runes in this.primaryPerks().slots' class="perks">
        <div v-for='rune in runes.runes' class="perk">
            <img class='inactive' :src="'https://ddragon.leagueoflegends.com/cdn/img/' + rune.icon" alt="">
        </div>
    </div>
</div>

Answer №1

Is it possible that the match object appears like this at a certain point?

data(){
    return {
        match: {
           perk0: 8112,
           perk1: 8113,
           perk2: 8115,
           perk3: 8122,
        }
    }
},

If that's the case, here is how to conditionally apply a class.

<div class="runes">
    <div v-for='runes in this.primaryPerks().slots' class="perks">
        <div v-for='(rune,index) in runes.runes' class="perk">
            <img :class="{inactive: Object.values(match).find(rune.id)}" :src="'https://ddragon.leagueoflegends.com/cdn/img/' + rune.icon" alt="">
        </div>
    </div>
</div>

I recommend creating a method for checking runes that returns true or false and accepts the id as a parameter.

checkRune(id) {
    return Object.values(match).find(id)
}

Then your class should be like

:class="{inactive: checkRune(rune.id)}

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

Creating Unique Numbers for Every <a> Element

Can someone help me figure out how to create a form that generates a unique set of random numbers from the range (0,9) displayed in a group of button tags? I've written some javascript code but it's not quite working as intended. (function($) ...

Prevent a child DIV from adopting the style of its parent page

Imagine a scenario where you have a webpage with its own unique stylesheet. You decide to include a popup or modal at the end of the page using Javascript, but you want this popup to have its own distinct style. Since the content within the modal is dynami ...

Sharing methods between two components on the same page in Angular can greatly improve code efficiency

On a single page, I have two components sharing some methods and properties. How can I utilize a service to achieve this? See the sample code snippet below... @Component({ selector: 'app', template: '<h1>AppComponent1</h1>' ...

Navigating events with Electron and Vue (Single Page Application)

I've been struggling to understand how to handle events in Vue with Electron. Despite reading the documentation and testing Vue instances and directives in the browser successfully, I can't seem to get it working in my Electron desktop app (which ...

Troubleshooting issue with Bootstrap's responsive scrollspy functionality

I am experiencing an issue with the responsiveness of my page. When I reduce the width of the page to half, the scrollspy disappears unexpectedly. I am unsure about the root cause of this problem. If you run the code I have shared here, you can observe th ...

Dropdown Pattern with React CTA Modal

While using MaterialUI's ButtonGroup for a dropdown menu, I encountered an issue trying to set up a series of CTAs that are easily interchangeable within it. The goal is to have all components reusable and the choices in the dropdown dynamic. const C ...

Issue encountered with updating canvas texture twice in A-Frame and pdf.js while operating in virtual reality mode

Using the power of A-Frame and pdf.js, I embarked on a quest to build an incredible VR application for viewing PDF files. Everything seemed to be working seamlessly on my desktop - flipping through PDF pages, rendering them onto a canvas, it was a sight to ...

Switch Between Different Background Colors for Table Rows With Each Click

This script changes colors when a row in a specific column is clicked: $(document).ready(function(){ $("#rowClick").children("tbody").children("tr").children("td").click(function(){ $(this.parentNode).toggleClass("enroute"); }); }); CSS: .placed{ b ...

Transferring information from the service layer to the controller

My goal is to transfer data from a service to a controller using AngularJS. Below is the code I am using: .controller('lista',function($scope,cetrams){ $scope.hola="This is it"; var test; var testfn = function(){ test = &apo ...

What is the best way to ensure the constant rotation speed of this simple cube demo?

Currently delving into the world of Three.js. I'm curious about how to make the cube in this demo rotate at a consistent speed rather than depending on mouse interactions. Any tips on achieving this? ...

How can you trigger a page method from a layout event in Vue.js?

I'm working on a design that includes a sidebar and an image gallery on the page. Initially, all images are loaded without any filters applied. However, when a button in the sidebar is clicked, I want the page to display filtered images. Although I ca ...

Having trouble with the Aurelia JSPM install -y command not functioning properly on Windows

I am currently following the Aurelia tutorial at I am attempting to install Aurelia dependencies using Gulp and JSPM. I successfully ran "jspm install -y" without any issues. However, upon opening the browser console, I encountered the following error: ...

Implementing stop loss with node-binance-api: A step-by-step guide

Currently utilizing node-binance-api for trading purposes. I have initiated an order by executing the following lines of code: let adjustLeverage = await binance.futuresLeverage(coin, 2); let res_2 = await binance.futuresMarketSell(coin, quantity); . Subs ...

Attempting to create a functional action listener for a deck of cards game

I'm currently working on a game and want to make an image appear blank when clicked on, to simulate it disappearing. Specifically, this is for a tri peaks solitaire game. I have a function that tests the validity of playing a card, but I'm strugg ...

Choose the initial item from a dropdown menu in Angular 7

I have a dropdown list containing data in the form of a Map<number, string>. By default, the data is sorted based on the keys in ascending order. When displaying this data in HTML, I am using the keyvalue pipe and a function to sort the items by the ...

Issue with Durandal dialog repositioning failing to function

I've been attempting to adjust the positioning of a Durandal dialog, but have been unsuccessful so far. The code snippet I'm using is as follows: this.compositionComplete = function (child, parent, context) { dialog.getContext().reposition(c ...

`Passing JavaScript variables through HTML anchor tags`

I am currently attempting to pass the id to the controller using this method: {{ route("admin.service.edit", '+val[0]+' )}} However, the '+val[0]+' is being interpreted as a string in the URL http://localhost:8000/admin/servi ...

Encountered error: "Node.js and socket.io: Address already in use"

Experimenting with chat using Node.js and socket.io Currently, I am running Ubuntu 12.04 as a user and have a folder "pp" on my desktop. In this folder, I have placed a server file named server.js. Below is the client code: $(document).ready(function() ...

The retrieval of data from AWS Dynamodb in Node.js is not done synchronously

I recently started working with Node.js and DynamoDB. I created a Node.js SDK to retrieve a single row from a DynamoDB table. The data is being fetched correctly, but there is a delay which is causing an error. Below is a snippet of my code: var AWS = re ...

ReactJS mixes up fetch URLs with another fetch_WRONG_FETCH

I have a fetch function in my Home component: export default function Home() { const { rootUrl } = useContext(UserContext); useEffect(() => { fetch(`${rootUrl}/api/products/featuredProducts`) .then((result) => result.json()) .then ...