当前位置:网站首页>Implement an array in the form of JS function prototype. forEach(),. map(),. filter()
Implement an array in the form of JS function prototype. forEach(),. map(),. filter()
2022-04-21 07:15:00 【cornivores】
Array.prototype.forEach(),.map(),.filter() The three difference
- forEach(),map(),filter() All three implement the iteration of array elements
- forEach(),map() The difference is very small , The only difference is forEach() No return value , and map() There is a return value , Returns a new array
- filter() Returns the array filtered by the function
Array.prototype.forEach()
Use pure for Cycle writing forEach The effect of ,forEach The efficacy is shown in the figure :

The implementation code is as follows :
Array.prototype.forEach = function(callback, thisArg) {
// Determine whether the first parameter is a function
if(typeof callback != 'function') throw new TypeError(callback + 'must be a function')
var arr = this
// Whether the second parameter is received
var thisValue = arguments[1] || this
for(let i = 0; i < arr.length; ++i) {
// change this The direction of
callback.call(thisValue, arr[i], i, arr)
}
}
Array.prototype.map()
map And forEach The effect is similar ,map The efficacy is as follows :

The code is as follows :
Array.prototype.map = function(callback, thisArg) {
// Determine whether the first parameter is a function
if(typeof callback != 'function') throw new TypeError(callback + 'must be a function')
var arr = this
// Whether the second parameter is received
var thisValue = arguments[1] || this
// Save return value
let returnValue = []
for(let i = 0; i < arr.length; ++i) {
// change this The direction of + Push the return value
if(callback.call(thisValue, arr[i], i, arr))
returnValue.push(arr[i])
}
return returnValue
}
Array.prototype.filter()
filter() The efficacy is as follows :

The code is as follows :
Array.prototype.filter = function(callback, thisArg) {
// Determine whether the first parameter is a function
if(typeof callback != 'function') throw new TypeError(callback + 'must be a function')
var arr = this
// Whether the second parameter is received
var thisValue = arguments[1] || this
// Save return value
let returnValue = []
for(let i = 0; i < arr.length; ++i) {
// The value matches the original array
if(callback.call(thisValue, arr[i], i, arr))
returnValue.push(arr[i])
}
return returnValue
}
版权声明
本文为[cornivores]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210615591393.html
边栏推荐
猜你喜欢

语义的特征提取及简单词频展示(WordCloud)

第五章 支持向量机(SVM)

【SSM整合】1. 基本环境搭建

微服务——服务拆分策略与原则

MySQL Workbench cannot use clear text authentication over non-ssl connections 问题解决

Sql 按照指定内容排序
![[STM32] cubemx configuration diagram of 480mhz clock under 25MHz external crystal oscillator of h743](/img/7c/f7bc594f530ba44d59e365279021f3.png)
[STM32] cubemx configuration diagram of 480mhz clock under 25MHz external crystal oscillator of h743

记一次mySQL慢sql优化
![[SSM integration] 1 Basic environment construction](/img/f9/816e05e0246bcbfe101f64cf59fa5a.png)
[SSM integration] 1 Basic environment construction

Swagger2生成Api文档
随机推荐
Multi file upload (form submission)
为短路运算符布尔表达式添加括号
6 service and ingress
【SSM整合】4. 逻辑代码编写以及测试
3.bat中date命令问题
imx6网口上电后连接不稳定link up,link down
Tensorflow example 3: recognition training of verification code pictures. Each picture has 4 letters
Write tweets for one year and share five commonly used writing software
[WPF] notes
[AD] modular schematic drawing pit point record
img= img.copy()报错
stm32mp157 wm8960音频驱动调试笔记
CANopen STM32移植
[SSM integration] 4 Logic code writing and testing
mysql与oracle的区别小总结
语义的特征提取及简单词频展示(WordCloud)
3、 2 [Verilog HDL] hierarchical modeling of basic knowledge
Semantic feature extraction and simple word frequency display (wordcloud)
jvm 的 解释执行与 编译执行
Flat (flat lattice transformer) code implementation