当前位置:网站首页>Implement the foreach method of array
Implement the foreach method of array
2022-07-03 00:15:00 【heiheiheiheiheiheii】
Array of forEach Method is used to traverse the array , no return value .
let arr = [1, 2, 3, 4, 5];
arr.forEach((item, index, arr) => {
console.log(index + ':' + item, arr);
})
0:1 (5) [1, 2, 3, 4, 5]
1:2 (5) [1, 2, 3, 4, 5]
2:3 (5) [1, 2, 3, 4, 5]
3:4 (5) [1, 2, 3, 4, 5]
4:5 (5) [1, 2, 3, 4, 5]IE Medium Array There is no type forEach() Method , Then let's realize one forEach Method .
The first version
if (!Array.prototype.forEach1) {
Array.prototype.forEach1 = function (callback, thisArg) {
var _this, index;
if (!Array.isArray(this)) {
throw new TypeError(" Can only be used with arrays forEach Method ");
}
if (Object.prototype.toString.call(callback) != "[object Function]") {
throw new TypeError('callback It has to be a function ');
}
if (thisArg) {
_this = thisArg;
}
for (index = 0; index < this.length; index++) {
callback.call(_this, this[index], index, this);
}
}
}But for loose arrays ,arr = [ , , , , ,] To use forEach You can't get results . Like we use above length To cycle , Yes, it will print out undefined Of , arr[i] Can't find it will return undefined.
So we need to change .
The second edition .
if (!Array.prototype.froEach) {
Array.prototype.froEach = function(callback, thisArg) {
let _this, k = 0;
if (!Array.isArray(this)) {
throw new TypeError(" Can only be used with arrays forEach Method ");
}
var O = Object(this);
var len = this.length;
if (Object.prototype.toString.call(callback) != "[object Function]") {
throw new TypeError('callback It has to be a function ');
}
if (thisArg) {
_this = thisArg;
}
while (k < len) {
var kValue;
if (k in O) {
kValue = O[k];
callback.call(T, kValue, k, O);
}
k++;
}
};
}边栏推荐
- Flexible combination of applications is a false proposition that has existed for 40 years
- Xcode real machine debugging
- 带角度的检测框 | 校准的深度特征用于目标检测(附实现源码)
- Interpretation of new plug-ins | how to enhance authentication capability with forward auth
- Leetcode DP three step problem
- Slf4j + Logback日志框架
- cocospods 的使用
- JSON data transfer parameters
- Judge whether the binary tree is full binary tree
- 开发知识点
猜你喜欢

Optimization of streaming media technology

Which websites can I search for references when writing a thesis?

MySQL Foundation

基于OpenCV实现口罩识别

67 page overall planning and construction plan for a new smart city (download attached)

Digital twin visualization solution digital twin visualization 3D platform

程序分析与优化 - 9 附录 XLA的缓冲区指派

带角度的检测框 | 校准的深度特征用于目标检测(附实现源码)

Matlab 信号处理【问答笔记-1】

Request and response
随机推荐
What is the official website address of e-mail? Explanation of the login entry of the official website address of enterprise e-mail
Explain in detail the process of realizing Chinese text classification by CNN
cocospods 的使用
Practical series - free commercial video material library
论文的设计方案咋写?
[shutter] shutter open source project reference
SQL query statement parameters are written successfully
Using tensorflow to realize voiceprint recognition
洛谷_P2010 [NOIP2016 普及组] 回文日期_折半枚举
Custom throttling function six steps to deal with complex requirements
大学生课堂作业2000~3000字的小论文,标准格式是什么?
開源了 | 文心大模型ERNIE-Tiny輕量化技術,又准又快,效果全開
Create an interactive experience of popular games, and learn about the real-time voice of paileyun unity
How QT exports data to PDF files (qpdfwriter User Guide)
基于OpenCV实现口罩识别
Bean加载控制
Matlab 信号处理【问答笔记-1】
What are the projects of metauniverse and what are the companies of metauniverse
实用系列丨免费可商用视频素材库
JVM foundation review