高阶函数的运用

需求:取出所有小于100的数字

函数式编程:命令式编程/声明式编程

编程范式:面向对象编程(第一公民:对象)/函数式编程(第一公民:函数)

下面是普通写法

const nums = [10, 20, 30, 40, 554];
let newNums = [];
for (let n of nums) {
    if (n < 100) {
        newNums.push[n];
    }
}
//2.将所有小于100的数字进行转化:全部*2
let new2Nums = [];
for (let n of newNums) {
    new2Nums.push(n * 2);
}
console.log(new2Nums);
// 3..将所有的数字相加,得到最终的结果
let total=0;
for(let n of new2Nums){
   total+=n;
}
console.log(total)

接下来是高阶函数的应用

//filter函数的使用
const nums = [10, 20, 30, 40, 554];
//过滤  会回调五次
//filter中的回调函数有一个要求:必须返回一个布尔值
//true:当返回true时,函数内部会自动将这次回调的n加入到新的数组中
//false:当返回false时,函数内部会过滤到这次n

let newNums =nums.filter(function(n){
  return n<100;
})
 //console.log(newNums)


//map的函数的使用 
let new2Nums=newNums.map(function (n){
    return n*2;
})
console.log(new2Nums)

//3.reduce函数的使用
//reduce作用对数组中所有的内容进行汇总
// new2Nums.reduce(function(preValue,n){
//    return 100;
// },0)
// //第一次:preValue是0(因为初始化为0) n为20(数组中的第一个值)
// //第二次:preValue是100(因为初始化为0) n为40(数组中的第二个值)
// //第二次:preValue是100(因为初始化为0) n为60(数组中的第二个值)
// //第二次:preValue是100(因为初始化为0) n为80(数组中的第二个值)
let total=new2Nums.reduce(function(preValue,n){
    return preValue+n;
 },0)
 console.log(total);
 //第一次:preValue是0(因为初始化为0) n为20(数组中的第一个值)
 //第二次:preValue是100(因为初始化为20) n为40(数组中的第二个值)
 //第三次:preValue是100(因为初始化为60) n为60(数组中的第二个值)
 //第四次:preValue是100(因为初始化为120) n为80(数组中的第二个值)
 //200

更简便的写法

const nums2 = [10, 20, 30, 40, 554];
let total2=nums.filter(function(n){
    return n<100;
}).map(function(n){
    return n*2;
}).reduce(function(preValue,n){
    return preValue+n;
},0)
console.log(total2);
//更离谱的简便写法
let total3=nums.filter(n=> n<100).map(n=> n*2).reduce((pre,n)=>pre+n);
console.log(total3)

 

运行结果

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
React 高阶函数可以在许多场景中运用,以下是一些常见的应用示例: 1. 权限控制:通过高阶函数可以实现对组件的访问权限控制。例如,可以创建一个名为 `withAuth` 的高阶函数,它根据用户的登录状态来决定是否渲染原始组件。 ```jsx function withAuth(WrappedComponent) { return function WithAuth(props) { const isLoggedIn = checkUserLoginStatus(); // 检查用户登录状态的函数 if (isLoggedIn) { return <WrappedComponent {...props} />; } else { return <div>请先登录</div>; } } } const MyComponent = () => <div>需要登录才能看到的内容</div>; const AuthComponent = withAuth(MyComponent); // 使用增强后的组件 <AuthComponent /> ``` 2. 数据获取:通过高阶函数可以封装数据请求逻辑,使组件可以方便地获取数据并进行渲染。例如,可以创建一个名为 `withDataFetching` 的高阶函数,它负责从 API 获取数据,并将数据作为 props 传递给原始组件。 ```jsx function withDataFetching(WrappedComponent, url) { return function WithDataFetching(props) { const [data, setData] = useState(null); useEffect(() => { fetchData(); }, []); async function fetchData() { try { const response = await fetch(url); const jsonData = await response.json(); setData(jsonData); } catch (error) { console.error('Error fetching data:', error); } } return <WrappedComponent {...props} data={data} />; } } const MyComponent = ({ data }) => { if (!data) { return <div>Loading...</div>; } return ( <ul> {data.map(item => ( <li key={item.id}>{item.name}</li> ))} </ul> ); }; const DataComponent = withDataFetching(MyComponent, 'https://api.example.com/data'); // 使用增强后的组件 <DataComponent /> ``` 3. 状态管理:通过高阶函数可以封装一些公共状态,使多个组件可以共享该状态。例如,可以创建一个名为 `withTheme` 的高阶函数,它将主题信息作为 props 传递给原始组件。 ```jsx function withTheme(WrappedComponent) { return function WithTheme(props) { const theme = useContext(ThemeContext); // 使用 Context 获取主题信息 return <WrappedComponent {...props} theme={theme} />; } } const MyComponent = ({ theme }) => ( <div style={{ background: theme.background, color: theme.text }}> 主题样式 </div> ); const ThemedComponent = withTheme(MyComponent); // 使用增强后的组件 <ThemedComponent /> ``` 这些只是一些常见的应用场景,实际上,React 高阶函数非常灵活,可以根据具体需求来定义和使用。它们可以帮助我们在不改变原始组件的情况下,增加、修改或封装组件的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值