当前位置:网站首页>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++;
}
};
}边栏推荐
- yolov5train. py
- Digital twin smart factory develops digital twin factory solutions
- CADD课程学习(4)-- 获取没有晶体结构的蛋白(SWISS-Model)
- 请求与响应
- 67 page overall planning and construction plan for a new smart city (download attached)
- JDBC tutorial
- cocospods 的使用
- 有哪些比较推荐的论文翻译软件?
- Chapter 3 of getting started with MySQL: database creation and operation
- yolov5test. Py comment
猜你喜欢

请问大家在什么网站上能查到英文文献?

Open source | Wenxin big model Ernie tiny lightweight technology, which is accurate and fast, and the effect is fully open

開源了 | 文心大模型ERNIE-Tiny輕量化技術,又准又快,效果全開

What is the official website address of e-mail? Explanation of the login entry of the official website address of enterprise e-mail
![Luogu_ P2010 [noip2016 popularization group] reply date_ Half enumeration](/img/a3/55bb71d39801ceeee421a0c8ded333.png)
Luogu_ P2010 [noip2016 popularization group] reply date_ Half enumeration

Program analysis and Optimization - 9 appendix XLA buffer assignment

The privatization deployment of SaaS services is the most efficient | cloud efficiency engineer points north

教育学大佬是怎么找外文参考文献的?

Bean load control

Digital twin visualization solution digital twin visualization 3D platform
随机推荐
Define MySQL function to realize multi module call
yolov5detect. Py comment
Which software can translate an English paper in its entirety?
经济学外文文献在哪查?
Bean加载控制
MFC file operation
Which websites can I search for references when writing a thesis?
Open source | Wenxin big model Ernie tiny lightweight technology, which is accurate and fast, and the effect is fully open
MFC文件操作
Use of cocospods
How much do you know about synchronized?
MySQL基础
Master the development of facial expression recognition based on deep learning (based on paddlepaddle)
Top Devops tool chain inventory
What website can you find English literature on?
Architecture: load balancing
写论文可以去哪些网站搜索参考文献?
Open Source | Wenxin Big Model Ernie Tiny Lightweight Technology, Accurate and Fast, full Open Effect
QT 如何将数据导出成PDF文件(QPdfWriter 使用指南)
Where can I find foreign papers?