如何利用reduce来找出一个数字数组中最大值?代码如下:
const nums = [6,3,4];const maxNum = nums.reduce((max, num) => { if(num > max) { return num; } else { return max; }}, 0);console.log("maxNum", maxNum) //maxNum 6
也可以简写成:
const maxNum = nums.reduce((max, num) => (num > max ? num : max), 0);
还没有评论,来说两句吧...