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

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

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

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

提交回复
热议问题