【Javascript】数组常用方法及其手写代码

Array.prototype.forEach()

定义

forEach() 方法对数组的每个元素执行一次给定的函数。

语法

arr.forEach(callback(currentValue [, index [, array]])[, thisArg])

示例

手写一个forEach()

        Array.prototype.myForeach = function(fn) {
            if (!Object.prototype.toString.call(fn) === '[object Function]') {
                throw new Error(`${fn} is not a function`)
            }
            let param = this
            for (let i = 0; i < param.length; i++) {
                fn(param[i], i, param)
            }
        }

        let arr = ['a', 'b', 'c']
        arr.myForeach((item, index) => {
            console.log(item, index)
        });

Array.prototype.map()

定义

map() 方法创建一个新数组,其结果是该数组中的每个元素是调用一次提供的函数后的返回值。

语法

var new_array = arr.map(function callback(currentValue[, index[, array]]) {
 // Return element for new_array 
}[, thisArg])

示例

手写一个map()

        Array.prototype.myMap = function(fn) {
            if (!Object.prototype.toString.call(fn) === '[object Function]') {
                throw new Error(`${fn} is not a function`)
            }
            let param = this
            let newMap = []
            for (let i = 0; i < param.length; i++) {
                newMap.push(fn(param[i], i, param))
            }
            return newMap
        }

        let arr = ['a', 'b', 'c']
        let newArr = arr.myMap(item => {
            return item + item
        });
        console.log(newArr)

Array.prototype.filter()

定义

filter() 方法创建一个新数组, 其包含通过所提供函数实现的测试的所有元素。 

语法

var newArray = arr.filter(callback(element[, index[, array]])[, thisArg])

示例

手写一个filter()

        Array.prototype.myfilter = function(fn) {
            if (!Object.prototype.toString.call(fn) === '[object Function]') {
                throw new Error(`${fn} is not a function`)
            }
            let param = this
            let filterArr = []
            for (let i = 0; i < param.length; i++) {
                if (fn(param[i], i, param)) {
                    filterArr.push(param[i])
                }
            }
            return filterArr
        }

        let arr = ['a', 'b', 'c']
        let newArr = arr.myfilter(item => {
            return item === 'a'
        });
        console.log(newArr)

Array.prototype.every()

定义

every() 方法测试一个数组内的所有元素是否都能通过某个指定函数的测试。它返回一个布尔值。

注意:若收到一个空数组,此方法在一切情况下都会返回 true

语法

arr.every(callback(element[, index[, array]])[, thisArg]) 

示例

手写一个every()

        Array.prototype.myEvery = function(fn) {
            if (!Object.prototype.toString.call(fn) === '[object Function]') {
                throw new Error(`${fn} is not a function`)
            }
            let param = this
            for (let i = 0; i < param.length; i++) {
                if (!fn(param[i], i, param)) {
                    return false
                }
            }
            return true
        }

        let arr = ['a', 'a', 'a']
        let newArr = arr.myEvery(item => {
            return item === 'a'
        });
        console.log(newArr)

Array.prototype.some()

定义

some() 方法测试数组中是不是至少有1个元素通过了被提供的函数测试。它返回的是一个Boolean类型的值。

注意:如果用一个空数组进行测试,在任何情况下它返回的都是false

 语法

 arr.some(callback(element[, index[, array]])[, thisArg])

示例

手写一个some()

        Array.prototype.mySome = function(fn) {
            if (!Object.prototype.toString.call(fn) === '[object Function]') {
                throw new Error(`${fn} is not a function`)
            }
            let param = this
            for (let i = 0; i < param.length; i++) {
                if (fn(param[i], i, param)) {
                    return true
                }
            }
            return false
        }

        let arr = ['b', 'a', 'c']
        let newArr = arr.mySome(item => {
            return item === 'a'
        });
        console.log(newArr)

Array.prototype.find()

定义

 find() 方法返回数组中满足提供的测试函数的第一个元素的值。否则返回undefined。

示例

手写一个find()

    Array.prototype.myFind =function (fn) {
        if(!Object.prototype.toString.call(fn) === '[object Function]'){
            throw new Error(`${fn} is not a funcion`)
        }
        let param = this
        for(let i=0;i<param.length;i++){
            if(fn(param[i], i, param)) {
                return param[i]
            }
        }
    }
    console.log(arr.myFind(item => item > 2))

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值