当前位置:网站首页>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++;
}
};
}
边栏推荐
- ArrayList analysis 2: pits in ITR, listiterator, and sublist
- Define MySQL function to realize multi module call
- S12. Verify multi host SSH mutual access script based on key
- 判断二叉树是否为满二叉树
- 开发知识点
- How much do you know about synchronized?
- JVM foundation review
- 哪些软件可以整篇翻译英文论文?
- Angled detection frame | calibrated depth feature for target detection (with implementation source code)
- zhvoice
猜你喜欢
What are the projects of metauniverse and what are the companies of metauniverse
带角度的检测框 | 校准的深度特征用于目标检测(附实现源码)
Bean load control
The privatization deployment of SaaS services is the most efficient | cloud efficiency engineer points north
Xcode real machine debugging
Where can I find the English literature of the thesis (except HowNet)?
maya渔屋建模
Should you study kubernetes?
How do educators find foreign language references?
How to set automatic reply for mailbox and enterprise mailbox?
随机推荐
TypeError: Cannot read properties of undefined (reading ***)
AcWing_188. 武士风度的牛_bfs
Where can I find the English literature of the thesis (except HowNet)?
Mutual exclusion and synchronization of threads
Luogu_ P2010 [noip2016 popularization group] reply date_ Half enumeration
How to maintain the brand influence of clothing enterprises
CADD course learning (4) -- obtaining proteins without crystal structure (Swiss model)
MFC file operation
Slf4j + Logback日志框架
Sourcetree details
直击产业落地!飞桨重磅推出业界首个模型选型工具
Mapper agent development
秒杀系统设计
Linux 下安装 redis
请求与响应
ArrayList分析2 :Itr、ListIterator以及SubList中的坑
Leetcode skimming - game 280
MFC gets the current time
yolov5train. py
Flexible combination of applications is a false proposition that has existed for 40 years