javascript push() 在 reduce() 中不会按预期工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32722435/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 15:40:16  来源:igfitidea点击:

push() won't work as expected in reduce()

javascriptarraysreduce

提问by Lasse Karagiannis

Why doesn't a.push(b)work in my Array.reduce()? a=a.push(b)where bis a string, turns ato an integer.?!

为什么没有a.push(b)在我的工作,Array.reduce()? a=a.push(b)在那里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;  
 }                                  

回答by Barmar

Since push()returns the new length of the array, you're assigning the length to a. Instead of a conditional operator, use an ifstatement.

由于push()返回数组的新长度,因此您将长度分配给a. 使用if语句代替条件运算符。

var winner = objKeys.reduce((a, b) => {
    if (frequency[b] === highestVal?) {
        a.push(b);
    }
    return a;
}, []);

回答by Alexander Moiseyev

The push() returns the new length. You can use ES2015 spread syntax:

push() 返回新长度。您可以使用 ES2015 扩展语法:

var winner = objKeys.reduce((a, b)=> {
    a = (frequency[b] === highestVal)? [...a, b] : a;
    return a
}, []);

回答by alexi2

Building on the excellent answer by @Alexander Moiseyev.

基于@Alexander Moiseyev 的出色回答。

You can avoid setting and mutating both variables aand winnerby doing the following:

您可避免设置和不断变化的两个变量a,并winner通过执行以下操作:

return objKeys.reduce((acc, val) =>
    frequency[val] === highestVal 
      ? [...acc, val] 
      : acc
,[])

note: for clarity I have explicitly declared the accumulator and value in this reduce method.

注意:为了清楚起见,我在这个 reduce 方法中明确声明了累加器和值。

回答by Daniel Rosano

Please note that this structure you provide is not clear enough

请注意你提供的这个结构不够清晰

I would use instead an array of objects each having a name and a frecuency

我会使用一个对象数组,每个对象都有一个名称和一个频率

var frequencies = [{name : "mats", frecuency : 1},
                   {name : "john", frecuency: 3},
                   {name : "johan", frecuency: 2},
                   {name : "jacob", frecuency: 3}];

Then you can use a filter operation and map to get what you need

然后你可以使用过滤操作和映射来得到你需要的

var max = Math.max.apply(Math, frequencies.map(function(o){return o.frecuency;}));
var maxElems = frequencies.filter(function(a){return a.frecuency == max}).map(function(a){return a.name;});

maxElemswill give you the names of the people with higher frecuency

maxElems会给你更高的人的名字 frecuency