数组声明式系列方法:map、reduce、filter、some等的实现

抽象流星
• 阅读 986

map()


  Array.prototype.myMap = function (callback) {
    const arr = []
    // 遍历当前数组每个元素, 调用callback得到一个结果数据, 添加arr
    for (let index = 0; index < this.length; index++) {
      const element = this[index];
      const result = callback(element, index)
      arr.push(result)
    }
    return arr
  }

  // 定义数组 
  const arr = [55,22,11,33,44]
  //  map(callback),接收一个函数为参数,返回一个加工后的新数组
  const res = arr.myMap((item,index) => item + 10)
  // 打印
  console.log(res)

reduce()


  Array.prototype.myReduce = function (callback, initValue) {
    // 结果为初始值
    let total = initValue
    // 遍历当前数组每个元素, 调用callback得到结果数据,以供下一次计算
    for (let index = 0; index < this.length; index++) {
      const element = this[index];
      // 使用total保存每次回调返回的值
      total = callback(total, element, index)
    }

    // 返回结果
    return total
  }

  // 定义数组 
  const arr = [55,22,11,33,44]
  // 使用
  const res = arr.myReduce((pre,item) => pre *= item)
  // 打印
  console.log(res)

filter()


 Array.prototype.myFilter = function (callback) {
    const arr = []
    // 遍历当前数组每个元素
    for (let index = 0; index < this.length; index++) {
      const element = this[index];
      // 调用callback得到一个布尔值
      const result = callback(element, index)
      // 如果为true, 将当前element添加到arr
      if (result) {
        arr.push(element)
      }
    }
    // 返回这个数组
    return arr
    } 
    // 定义数组 
    const arr = [55,22,11,33,44]
    // 使用
    const resArr = arr.myFilter((item,index)=>{
      console.log(item,index)
      return item > 2
    }) 

    // 打印
    console.log(res)

find()


    Array.prototype.myFind = function(callback) {
      // 判断参数是否是function,如果不是就抛出提示
      if(typeof callback !== 'function') {
        throw 'argument must be a function'
      }
      // 循环遍历数组 this表示当前数组实例
      for(let i = 0; i < this.length; i++) {
        // 得到当前遍历元素
        const element = this[i]
        // 得到函数调用的返回值
        const result = callback(element,i)
        // 一找到就返回该元素
        if(result) return element
      }
      // 没有找到就返回undefined
      return undefined
    }
    // 定义数组 
    const arr = [55,22,11,33,44]
    // 使用
    const res = arr.myFind((item,index)=> item===3)
    // 打印
    console.log(res)

findIndex()


    Array.prototype.myFindIndex = function(callback) {
      // 判断参数是否是function,如果不是就抛出提示
      if(typeof callback !== 'function') {
        throw 'argument must be a function'
      }
      // 循环遍历数组 this表示当前数组实例
      for(let i = 0; i < this.length; i++) {
        // 得到当前遍历元素
        const element = this[i]
        // 得到函数调用的返回值
        const result = callback(element,i)
        // 一找到就返回该元素的下表
        if(result) return i
      }
      // 没有找到就返回-1
      return -1
    }
    // 定义数组 
    const arr = [55,22,11,33,44]
    // 使用
    let res = arr.myFindIndex((item,index)=> item===3)
    // 打印
    console.log(res)

every()


    Array.prototype.myEvery = function(callback) {
      // 判断参数是否是function,如果不是就抛出提示
      if(typeof callback !== 'function') {
        throw 'argument must be a function'
      }
      // 循环遍历数组 this表示当前数组实例
      for(let i = 0; i < this.length; i++) {
        // 得到当前遍历元素
        const element = this[i]
        // 得到函数调用的返回值
        const result = callback(element,i)
        // 有一个false就返回false
        if(!result) return false
      }
      // 都是true返回true
      return true
    }
    // 定义数组 
    const arr = [55,22,11,33,44]
    // 使用
    const res = arr.myEvery((item,index)=>{
      return item > 0
    })
    // 打印
    console.log(res)

some()


    Array.prototype.mySome = function(callback) {
      // 判断参数是否是function,如果不是就抛出提示
      if(typeof callback !== 'function') {
        throw 'argument must be a function'
      }
      // 循环遍历数组 this表示当前数组实例
      for(let i = 0; i < this.length; i++) {
        // 得到当前遍历元素
        const element = this[i]
        // 得到函数调用的返回值
        const result = callback(element,i)
        // 如果有就返回true
        if(result) return true
      }
      // 都没有返回false
      return false
    }
    // 定义数组 
    const arr = [55,22,11,33,44]
    // 使用
    const res = arr.mySome((item,index)=>{
      return item = 2
    })
    // 打印
    console.log(res)

forEach()


    Array.prototype.myForEach = function(callback) {
      // 判断参数是否是function,如果不是就抛出提示
      if(typeof callback !== 'function') {
        throw 'argument must be a function'
      }
      // 循环遍历数组 this表示当前数组实例
      for(let i = 0; i < this.length; i++) {
        const element = this[i]
        callback(element,i)
      }
    }
    // 定义数组 
    const arr = [55,22,11,33,44]
    // 使用
    arr.myForEach((item,index)=>{
      console.log(item,index)
    })
    // 打印
    console.log(res)
点赞
收藏
评论区
推荐文章
blmius blmius
4年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
美凌格栋栋酱 美凌格栋栋酱
7个月前
Oracle 分组与拼接字符串同时使用
SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Dax Dax
4年前
js高频手写大全
1.手写instanceofinstanceof作用:判断一个实例是否是其父类或者祖先类型的实例。instanceof在查找的过程中会遍历左边变量的原型链,直到找到右边变量的prototype查找失败,返回false2.实现数组的map方法3.reduce实现数组的map方法4.手写数组的reduce方法reduce()方法接收一个函数作为累
凯特林 凯特林
4年前
JS - 用 for 循环实现常见的数组迭代方法
常见的数组迭代方法有很多种,比如some,filter,map等等,底层也都可以用for来实现,我们来康一康。somejsconstsome(arr,fn)for(leti0;i<arr.length;i)if(fn(arri,i,arr))re
Symbol卢 Symbol卢
4年前
js数组的遍历
js中数组的遍历1\.Map()方法Map映射的意思,可以看做是一个映射函数,所谓的映射。一个萝卜一个坑,一一对应的关系;语法:constarrArray();arr.map((item,index,arr)//函数体return;);//参数1:数组中的每一项(必选)//参数2:索引(可选)//参数3:当前遍历的数组本身(可选)//注
住儿 住儿
2年前
JS的一些优雅写法
JS的一些优雅写法reduce1、可以使用reduce方法来实现对象数组中根据某一key值求和例如,假设有以下对象数组:constarr其中,reduce方法的第一个参数是一个回调函数,它接收两个参数:累加器(acc)和当前元素(cur)。回调函数的返回值
Souleigh ✨ Souleigh ✨
4年前
Swift 常见面试题解
基础1、class和struct的区别2、不通过继承,代码复用(共享)的方式有哪些3、Set独有的方法有哪些?4、实现一个min函数,返回两个元素较小的元素5、map、filter、reduce的作用6、map与flatmap的区别7、什么是copyonwrite8、如何获取当前代码的函
Wesley13 Wesley13
3年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Stella981 Stella981
3年前
JavaScript遍历循环
定义一个数组和对象constarr'a','b','c','d','e','f';constobj{a:1,b:2,c:3,d:4}for()经常用来遍历数组元素遍历值为数组元素索引f
达里尔 达里尔
1年前
给数组添加新数据,判断数据是否重复
多选要进行数组拼接,希望判断往原数组里添的新数据是否重复,封装个简易方法languageconstdataArrayname:'aaa',id:1,name:'bbb',id:2;constnewDataname:'ccc',id:2;//要添加的新数
抽象流星
抽象流星
Lv1
你一句春不晚,我就到了真江南
文章
4
粉丝
0
获赞
0