【发布时间】:2020-04-27 10:10:44
【问题描述】:
我很难为计算不足的函数使用参数。对于这个例子,我有一个对象数组,每个对象都是一个游戏(棋盘游戏、视频游戏等)。我想显示我数组中游戏的一些统计数据,例如玩的总游戏、玩的总小时数、视频游戏的游戏数量、所有游戏的总价格、平均游戏评分等。
我的每个统计信息的模板如下所示:
<div class="game-stat">
<p>{{ gamesPlayedCounter }}/{{ games.length }}</p>
</div>
然后在我的计算中我会有一个类似的函数:
gamesPlayedCounter:function(){
var result = gameList.reduce((res, item) => item.status == 'played' ? res + 1 : res, 0);
return result;
}
问题是,我的很多统计数据都遵循同样的模式。如果我想计算电子游戏的数量,我可以这样做:
gamesTypeCounter:function(){
var result = gameList.reduce((res, item) => item.type == 'video-game' ? res + 1 : res, 0);
return result;
}
因此,这给了我所有具有视频游戏类型的游戏的总数。我最终得到了其中的 10 个,用于不同的统计数据。我知道我应该只用一个函数就能做到这一点并传入参数。
我认为我可以添加参数并将 item.type 切换为我想要检查的内容,然后将 'video-game' 切换为我想要检查的值,但这不起作用。这是我尝试过的:
<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;
}
这不起作用,所以现在我不确定如何创建一个在 Vue 中给定特定键和值时计数的函数。我什至应该为此使用计算吗?任何见解将不胜感激!
【问题讨论】:
-
这能回答你的问题吗? Dynamically access object property using variable。使用
item[key]而不是item.key,这与item["key"]相同——这与Vue 无关。另一种解决方案是简单地将一个循环中所有键的值相加。或gameList.filter(e => e[key] === value).length.
标签: javascript vue.js