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

后端 未结 4 500
悲&欢浪女
悲&欢浪女 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:16

    Building on the excellent answer by @Alexander Moiseyev.

    You can avoid setting and mutating both variables a and winner by doing the following:

    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.

提交回复
热议问题