我有以下方法找到信用额度最高的用户
let highest = this.users.reduce((max, current) => {
current.credits > max.credits ? current : max, {credits: 0}
});
现在我想知道我怎么能看到谁拥有最高(积分+投注)
let bestSupporter = this.users.reduce((max, current) => {
(current.credits + current.shots) > (max.credits + max.shots) ? current : max, {credits: 0}
});
以下也无效(将镜头添加到初始值
let bestSupporter = this.users.reduce((max, current) => {
(current.credits + current.shots) > (max.credits + max.shots) ? current : max, {credits: 0, shots: 0}
});
答案 0 :(得分:3)
在箭头函数中使用花括号,您需要@Factory
语句。
然后,您可以使用起始值来检查值
return
或省略起始值并直接使用对象。
let highest = this.users.reduce((max, current) => {
return current.credits > max.credits ? current : max,
}, { credits: 0 });
答案 1 :(得分:2)
我认为你的逻辑没问题。
你没有返回新的累积器。
let bestSupporter = this.users.reduce((max, current) => {
return (current.credits + current.shots) > (max.credits + max.shots) ? current : max, {credits: 0}
}, {/*Initialize a value for your accumulator*/}});
如果您不想在箭头功能中使用return
,请删除大括号
let bestSupporter = this.users.reduce((max, current) =>
(current.credits + current.shots) > (max.credits + max.shots) ? current : max, {credits: 0}
, {/*Initialize a value for your accumulator*/}});