reduce函数,reduce函数python

首页 > 教育 > 作者:YD1662024-04-07 21:10:51

5.最大值

const max = (arr) => arr.reduce((acc, curr) => Math.max(acc, curr), Number.NEGATIVE_INFINITY); let arr = [1, 2, 3, 4, 5]; console.log("参数为:", arr); console.log("结果为:", max(arr));

reduce函数,reduce函数python(5)

6.最小值

const min = (arr) => arr.reduce((acc, curr) => Math.min(acc, curr), Number.POSITIVE_INFINITY); let arr = [1, 2, 3, 4, 5]; console.log("参数为:", arr); console.log("结果为:", min(arr));

reduce函数,reduce函数python(6)

7.阶乘函数

const factorial = (n) => Array.from({ length: n }, (_, i) => i 1).reduce((acc, curr) => acc * curr, 1); // Array.from({ length: n }, (_, i) => i 1) 生成数组数据 [1,2,3,4,5] console.log("参数为:", 5); console.log("结果为:", factorial(5));

reduce函数,reduce函数python(7)

8.数组去重

const unique = (arr) => arr.reduce((acc, curr) => acc.includes(curr) ? acc : [...acc, curr], []); let arr = [1, 2,3,4,2, 3, 4, 5]; console.log("参数为:", arr); console.log("结果为:", unique(arr));

reduce函数,reduce函数python(8)

上一页12345下一页

栏目热文

文档排行

本站推荐

Copyright © 2018 - 2021 www.yd166.com., All Rights Reserved.