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

后端 未结 4 514
悲&欢浪女
悲&欢浪女 2021-01-17 08:48

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



        
4条回答
  •  青春惊慌失措
    2021-01-17 09:05

    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;});
    

    maxElems will give you the names of the people with higher frecuency

提交回复
热议问题