当前位置:网站首页>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++;
}
};
}边栏推荐
- How QT exports data to PDF files (qpdfwriter User Guide)
- 来自数砖大佬的 130页 PPT 深入介绍 Apache Spark 3.2 & 3.3 新功能
- Practical series - free commercial video material library
- Should you study kubernetes?
- Chapter 3 of getting started with MySQL: database creation and operation
- Missing number
- 有哪些比较推荐的论文翻译软件?
- Improvement of RTP receiving and sending PS stream tool (II)
- Talk with the interviewer about the pit of MySQL sorting (including: duplicate data problem in order by limit page)
- Mapper agent development
猜你喜欢
![[shutter] shutter open source project reference](/img/3f/b1d4edd8f8e8fd8e6b39548448270d.jpg)
[shutter] shutter open source project reference

Maybe you read a fake Tianlong eight

Digital collection trading website domestic digital collection trading platform
![[Verilog tutorial]](/img/15/d5e188a15e22fa44f1756fc492099d.jpg)
[Verilog tutorial]

How QT exports data to PDF files (qpdfwriter User Guide)

Explain in detail the process of realizing Chinese text classification by CNN

67页新型智慧城市整体规划建设方案(附下载)

MySQL基础

QT 如何将数据导出成PDF文件(QPdfWriter 使用指南)

Master the development of facial expression recognition based on deep learning (based on paddlepaddle)
随机推荐
Using tensorflow to realize voiceprint recognition
[reading notes] phased summary of writing reading notes
Open source | Wenxin big model Ernie tiny lightweight technology, which is accurate and fast, and the effect is fully open
Unique line of "Gelu"
SQL query statement parameters are written successfully
MFC文件操作
Create an interactive experience of popular games, and learn about the real-time voice of paileyun unity
Realization of mask recognition based on OpenCV
How much do you know about synchronized?
[shutter] open the third-party shutter project
Interface difference test - diffy tool
TypeError: Cannot read properties of undefined (reading ***)
基于OpenCV实现口罩识别
Chapter 3 of getting started with MySQL: database creation and operation
leetcode 650. 2 Keys Keyboard 只有两个键的键盘(中等)
Digital twin visualization solution digital twin visualization 3D platform
How do educators find foreign language references?
容器运行时分析
35 pages dangerous chemicals safety management platform solution 2022 Edition
Go自定义排序