当前位置:网站首页>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++;
}
};
}边栏推荐
- What is the standard format of a 2000-3000 word essay for college students' classroom homework?
- leetcode 650. 2 Keys Keyboard 只有两个键的键盘(中等)
- JVM foundation review
- Architecture: database architecture design
- 返回二叉树两个节点间的最大距离
- Bean load control
- [array] binary search
- Interpretation of new plug-ins | how to enhance authentication capability with forward auth
- 返回二叉树中最大的二叉搜索子树的根节点
- Linux 下安装 redis
猜你喜欢

130 pages of PPT from the brick boss introduces the new features of Apache spark 3.2 & 3.3 in depth

Dishes launcher small green program and directory management (efficiency tool)

Open Source | Wenxin Big Model Ernie Tiny Lightweight Technology, Accurate and Fast, full Open Effect

Many to one, one to many processing

JDBC練習案例

哪些软件可以整篇翻译英文论文?

直击产业落地!飞桨重磅推出业界首个模型选型工具

附加:token;(没写完,别看…)

实用系列丨免费可商用视频素材库

Mutual exclusion and synchronization of threads
随机推荐
Leetcode skimming - game 280
Unique line of "Gelu"
What is the standard format of a 2000-3000 word essay for college students' classroom homework?
130 pages of PPT from the brick boss introduces the new features of Apache spark 3.2 & 3.3 in depth
Is there a specific format for English papers?
Judge whether the binary tree is full binary tree
论文的设计方案咋写?
JDBC practice cases
How QT exports data to PDF files (qpdfwriter User Guide)
经济学外文文献在哪查?
maya渔屋建模
Chapter 4 of getting started with MySQL: data types stored in data tables
Returns the size of the largest binary search subtree in a binary tree
[array] binary search
Missing number
Explain in detail the process of realizing Chinese text classification by CNN
yolov5detect. Py comment
Maybe you read a fake Tianlong eight
Matlab 信号处理【问答笔记-1】
Hit the industry directly! The propeller launched the industry's first model selection tool