如何实现数组的map和filter方法

本文探讨如何实现JavaScript中数组的map和filter方法,解析这两个方法的内部原理,包括回调函数的作用域对象,并提供参考资料链接。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近经常看到如何实现原生方法的文章,之前自己对于这些并没有特别在意,看了几篇之后发现还是挺有意思的,搞清楚其中的原理比业务代码有趣多了。今天来记录一下数组的map和filter方法

参考资料:

Array.prototype.myMap

在实现map方法之前,有一个要注意的地方在于,map方法有一个很少用到的第二个参数,作为回调函数的作用域对象。即arr.map(fn(item, index, arr) => {}, obj)中的obj。虽然平时用得少,但是实现的时候要记得考虑进去。

// 一般实现
Array.prototype.myMap = function() {
	let arr = this;
	let result = [];
	let [fn, obj] = Array.prototype.slice.call(arguments);
	if (!arr instanceof Array) {
		throw new TypeError('Array only!');
	}
	if (typeof fn !== 'function') {
		throw new TypeError(`${fn} is not a function!`);
	}
	for (let i = 0; i < arr.length; i++) {
		const item = fn.call(obj, arr[i], i, arr);
		result.push(item);
	}
	return result
}

// reduce高端实现
Array.prototype.redecuMap = function(fn, thisArg) {
	return (list) => {
		// 不怎么愿意写下面这两个判断条件
		if (typeof fn !== 'function') {
			throw new TypeError(fn + 'is not a function')  
		}
		if (!Array.isArray(list)) {
			throw new TypeError('list must be a Array')
		}
		if (list.length === 0) return []
		return list.reduce((acc, value, index) => {
			return acc.concat([ fn.call(thisArg, value, index, list) ])
		}, [])
	}
}
// 上边是个高阶函数,返回的是另一个函数,所以调用时要这样:
reduceMap(x => x + 1)([ 1, 2, 3 ]) // [ 2, 3, 4 ]

const mapAry1 = reduceMap(function(item) {
    console.log(this)
    return item + 1
}, { msg: 'mapping' })([ 1, 2, 3 ]) 
// [ 2, 3, 4 ] 
// logging { msg: 'mapping' } three times

Array.prototype.myFilter

map类似的,filter也有第二个参数,实现了map后其他也基本没难度了。

// 一般实现
Array.prototype.myFilter = function() {
	let arr = this;
	let result = [];
	let [fn, obj] = Array.prototype.slice.call(arguments);
	if (!arr instanceof Array) {
		throw new TypeError('Array only!');
	}
	if (typeof fn !== 'function') {
		throw new TypeError(`${fn} is not a function!`);
	}
	for (let i = 0; i < arr.length; i++) {
		const validate = fn.call(obj, arr[i], i, arr);
		if(validate) {
			result.push(arr[i]);
		}
	}
	return result
}

// redece高端实现
Array.prototype.reduceFilter = function(fn, thisArg) {
	return (list) => {
		if (typeof fn !== 'function') {
			throw new TypeError(fn + 'is not a function')  
		}
		if (!Array.isArray(list)) {
			throw new TypeError('list must be a Array')
		}
		if (list.length === 0) return []
		return list.reduce((acc, value, index) => {
			const validate = fn.call(thisAry, value, index, list);
			return validate ? acc.concat([ value ]) : acc
		}, [])
	}    
}

// 上边是个高阶函数,返回的是另一个函数,所以调用时要这样:
reduceFilter(x => x % 2 === 0)([ 1, 2, 3 ]) // [ 2 ]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值