Javascript ES6使用reduce查找组合的多个值的最大值
我有下面的方法,可以找到信用最高的用户Javascript ES6使用reduce查找组合的多个值的最大值,javascript,ecmascript-6,Javascript,Ecmascript 6,我有下面的方法,可以找到信用最高的用户 let highest = this.users.reduce((max, current) => { current.credits > max.credits ? current : max, {credits: 0} }); 现在我想知道怎样才能知道谁的得分最高(积分+投篮) 我尝试了以下方法,但没有成功,我没有定义 以下操作也不起作用(将快照添加到初始值 let bestSupporter = this.users.reduc
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, shots: 0}
});
对于箭头函数中的花括号,您需要一个
return
语句
然后,可以使用起始值来检查该值
let highest = this.users.reduce((max, current) => {
return current.credits > max.credits ? current : max,
}, { credits: 0 });
或者忽略起始值并直接使用对象
let highest = this.users.reduce((a, b) => a.credits > b.credits ? a : b);
对于箭头函数中的花括号,您需要一个
return
语句
然后,可以使用起始值来检查该值
let highest = this.users.reduce((max, current) => {
return current.credits > max.credits ? current : max,
}, { credits: 0 });
或者忽略起始值并直接使用对象
let highest = this.users.reduce((a, b) => a.credits > b.credits ? a : b);
我想你的逻辑是正确的。 你不能退回新的蓄能器
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*/}});
如果您不想在arrow函数中使用返回
,请删除大括号
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*/}});
我想你的逻辑是正确的。 你不能退回新的蓄能器
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*/}});
如果您不想在arrow函数中使用返回
,请删除大括号
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*/}});
如果多个用户具有相同的最高值,会发生什么?这是一个好问题。我没有想到。我想先解决这个问题。如果多个用户具有相同的最高值,会发生什么情况?这是一个好问题。我没有想到。我想解决第一个问题。哈哈,我不知道花括号和返回值!比谢谢你。我不知道花括号和返回的东西!谢谢。啊哈第二个答案是使用a.credits+a.shots>b.credits+b.shotsha第二个答案是使用a.credits+a.shots>b.credits+b.shots