zoukankan      html  css  js  c++  java
  • 一些API回调函数转箭头函数写法

    刚开始接触箭头函数 不是很理解写法 想到之前用到的一些数组的API  于是把之前的回调用箭头函数改写了一下
    <script>
    let arr = [1,2,3,4,5,6,7,8,9,0]
    //  forEach  遍历数组  没有返回值
    let forEacha = arr.forEach((v, i) => {
        return arr[i] = v * 2;
    })
    //  map  遍历
    let maparr = arr.map((v, i) => {
        return arr[i] = v * 2
    })
        console.log(maparr)
    //  filter  筛选
    let arrf= arr.filter((v, i) => {
        return v > 5;
    })
        console.log(arrf)   
    //  reduce 累计器  累加累乘累除累模都行
    let reducea = arr.reduce((res,v) =>{
        return res + v
    }) 
        console.log(reducea) 
    //  find 筛选  返回数据
    let finda = arr.find((v,i) =>{
        return v > 20
    })
        console.log(arr)
        console.log(finda)
    //  findindex 筛选  返回下标
    let findindexa = arr.findIndex((v,i) =>{
        return v > 20
    })
        console.log(findindexa)
    </script>
  • 相关阅读:
    Fire
    Apple Tree
    访问艺术馆
    三角关系
    字母表
    折纸
    旅行
    单词分类

    圆桌游戏
  • 原文地址:https://www.cnblogs.com/tdtdttd/p/10838645.html
Copyright © 2011-2022 走看看