I am having a hard time using parameters for functions that I have under computed. For this example, I have an array of objects and each object is a game (board game, video game, etc). I want to show some stats about the games in my array like total games played, total hours played, how many games are video games, total price of all games, average game rating, etc.
My template for each statistic looks something like this:
<div class="game-stat">
<p>{{ gamesPlayedCounter }}/{{ games.length }}</p>
</div>
And then in my computed I would have a function like:
gamesPlayedCounter:function(){
var result = gameList.reduce((res, item) => item.status == 'played' ? res + 1 : res, 0);
return result;
}
The problem is, that a lot of my stats follow this same pattern. If I wanted to count the number of video games I could do this:
gamesTypeCounter:function(){
var result = gameList.reduce((res, item) => item.type == 'video-game' ? res + 1 : res, 0);
return result;
}
So this gives me the total for all of the games with the type of video game. And I end up with like 10 of these for different stats. I know I should be able to just do this with one function and pass in parameters.
I thought that I could just add parameters and switch out item.type with what I want to check and then 'video-game' with the value I want to check but that doesn't work. Here is what I tried:
<div class="game-stat">
<h4>Total Games Played:</h4>
<p>{{ counter('type', 'video-game') }}/{{ games.length }}</p>
</div>
counter:function(key, value){
var result = gameList.reduce((res, item) => item.key == value ? res + 1 : res, 0);
return result;
}
That doesn't work so now I am not sure how to create one function that counts when given a specific key and value in Vue. Should I even be using computed for this? Any insight would be appreciated!