Las*_*nis 14 javascript arrays reduce
为什么不在a.push(b)
我的Array.reduce()? a=a.push(b)
where b
字符串中工作,转为a
整数.?!
getHighestValuesInFrequency: function(frequency) {
//Input:var frequency = {mats: 1,john: 3,johan: 2,jacob: 3};
//Output should become ['John','jacob']
var objKeys = Object.keys(frequency);
var highestVal = objKeys.reduce((a,b)=>
{highestVal = (frequency[b] > a)? frequency[b] : a;
return highestVal;},0);
var winner = objKeys.reduce((a,b)=>
{ a = (frequency[b] === highestVal)? a.push(b) : a;
return a},[]);
return winner;
}
Run Code Online (Sandbox Code Playgroud)
Bar*_*mar 14
由于push()
返回数组的新长度,因此您需要指定长度a
.而不是条件运算符,使用if
语句.
var winner = objKeys.reduce((a, b) => {
if (frequency[b] === highestVal?) {
a.push(b);
}
return a;
}, []);
Run Code Online (Sandbox Code Playgroud)
小智 7
push()返回新的长度。您可以使用ES2015传播语法:
var Winner = objKeys.reduce((a,b)=> { a =(frequency [b] === maximumVal)?[... a,b]:a; 返回一个 },[]);
归档时间: |
|
查看次数: |
9146 次 |
最近记录: |