那些遍历数组的方法~

文章介绍了JavaScript中数组的几种重要实例方法,包括forEach用于遍历数组,map用于生成新数组,filter过滤数组生成新数组,find查找符合条件的第一个元素,findIndex查找元素的索引,以及every和some的元素检测,还有reduce用于累计处理的结果。文章通过自定义函数实现了这些方法,展示了它们的功能和用法差异。
摘要由CSDN通过智能技术生成

实例方法 forEach 用于遍历数组,替代 for 循环

 // 实现 myForEach 方法, 功能和 forEach 一模一样
        const arr = [1, 2, 3, 4]
        Array.prototype.myForEach = function (fn) {
            for (let i = 0; i < this.length; i++) {
                fn(this[i], i, this)
            }
        }
  // 遍历每一项
        arr.myForEach((item, index, arr) => console.log(item, index, arr))

实例方法 map 迭代原数组,生成新数组

 const arr = [1, 2, 3, 4]
// 实现 myMap 方法, 功能和 map 一模一样
        // 返回一个新数组
        Array.prototype.myMap = function (fn) {
            let newArr = []
            for (let i = 0; i < this.length; i++) {
                newArr.push(fn(this[i], i, this))
            }
            return newArr
        }
        const result = arr.myMap(item => `<span>${item}</span>`)

forEachmap 都是 JavaScript 中 Array 类型的方法,但它们主要的区别在于返回值和副作用。

forEach 方法是对数组中的每个元素执行一次指定的操作,没有返回值,它主要是通过副作用来改变代码的状态。例如,可以使用 forEach 方法来更新页面上的 DOM 元素,或者向服务器发送请求等。如果需要遍历数组并执行某个操作,但不需要创建新的数组,则可以使用 forEach


实例方法 filter 过滤数组单元值,生成新数组

       const arr = [1, 2, 3, 4]
 // 实现 myFilter 方法, 功能和 filter 一模一样
        // 返回一个满足条件新数组
        Array.prototype.myFilter = function (fn) {
            let newArr = []
            for (let i = 0; i < this.length; i++) {
                if (fn(this[i], i, this)) {
                    newArr.push(this[i])
                }
            }
            return newArr
        }
        // const result1 = arr.myFilter(function (item) {
        //     return item > 30
        // })
        const result1 = arr.myFilter(item => item > 1)
        console.log(result1);

实例方法 find 查找元素, 返回符合测试条件的第一个数组元素值,如果没有符合条件的则返回 undefined

 const arr = [1, 2, 3, 4]
// 实现 myFind 方法, 功能和 find 一模一样
        // 找到该元素后,不会继续查找其他元素。
        // 返回满足条件的元素
        Array.prototype.myFind = function (fn) {
            for (let i = 0; i < this.length; i++) {
                if (fn(this[i], i, this)) {
                    return this[i]
                }
            }
        }
        const result2 = arr.myFind(item => item > 2)
        console.log(result2);

实例方法 findIndex 查找元素的索引值

  const arr = [1, 2, 3, 4]
// 实现 myFindIndex 方法, 功能和 findIndex 一模一样
        // 找到该元素后,不会继续查找其他元素。
        // 返回满足条件的索引
        Array.prototype.myFindIndex = function (fn) {
            for (let i = 0; i < this.length; i++) {
                if (fn(this[i], i, this)) {
                    return i
                }
            }
            return -1
        }
        const result3 = arr.myFindIndex(item => item > 2)
        console.log(result3);

实例方法every 检测数组所有元素是否都符合指定条件,如果所有元素都通过检测返回 true,否则返回 false

 const arr = [1, 2, 3, 4]
// 实现 myEvery 方法, 功能和 every 一模一样
        // 返回值: 布尔值
        Array.prototype.myEvery = function (fn) {
            for (let i = 0; i < this.length; i++) {
                if (!fn(this[i], i, this)) {
                    return false
                }
            }
            return true
        }

        const result4 = arr.myEvery(item => item > 0)
        console.log(result4);

实例方法some 检测数组中的元素是否满足指定条件 如果数组中有元素满足条件返回 true,否则返回 false

const arr = [1, 2, 3, 4]
// 实现 mySome 方法, 功能和 some 一模一样
        // 返回值: 布尔值
        Array.prototype.myEvery = function (fn) {
            for (let i = 0; i < this.length; i++) {
                if (fn(this[i], i, this)) {
                    return true
                }
            }
            return false
        }

reduce 返回累计处理的结果,经常用于求和等

   const arr = [1, 2, 3, 4] 
// 实现 myReduce 方法, 功能和 reduce 一模一样 (必须传入初始值)
        // 返回值: 求和的结构
        Array.prototype.myReduce = function (fn, initValue) {
            for (let i = 0; i < this.length; i++) {
                initValue = fn(initValue, this[i], i, this)
            }
            return initValue
        }


        const result6 = arr.myReduce(function (prev, item) {
            return prev + item
        }, 0)
        console.log(result6);

findfilter的区别 返回值不一样, filter 返回的是数组 find 返回的是数组元素 查找方式不一样, filter是查找所有满足条件的数组元素, find只查找第一个满足条件的, 找到就终止查找

总结: find 是找元素, findIndex 是找索引, 使用方法完全一样 filter 是以筛选为目的的遍历, 而 find 是以找到元素为目的的遍历

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值