reduce函数,reduce函数python

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

17.去除空元素

const removeEmpty = (arr) => arr.reduce((acc, curr) => curr ? [...acc, curr] : acc, []); let arr = [0, 1, null, 2,undefined, '', 3] console.log("参数为:", arr); console.log("结果为:", removeEmpty(arr,"name"));

reduce函数,reduce函数python(17)

18.去除指定元素

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

reduce函数,reduce函数python(18)

19.元素映射

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

reduce函数,reduce函数python(19)

19.元素过滤

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

reduce函数,reduce函数python(20)

上一页12345下一页

栏目热文

文档排行

本站推荐

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