Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript ES6使用reduce查找组合的多个值的最大值_Javascript_Ecmascript 6 - Fatal编程技术网

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