当前位置:网站首页>Manually encapsulate a foreacht and map
Manually encapsulate a foreacht and map
2022-06-12 03:58:00 【Waste self-cultivation record】
Manually encapsulate an array method :forEach
function myForEach(arr,callback){
for(let i=0;i<arr.length;i++){
callback(arr[i],i);
}
}
myForEach([1,2,3],function(ele,index){
console.log(ele,index)
})
Mount prototype
Array.prototype.myForEach = function(callback) {
for (var i = 0; i < this.length; i++) {
// The first argument passed in during encapsulation is the value , The second argument is the index
callback(this[i], i);
}
};
[1, 2, 3].myForEach((ele, index) => console.log(ele, index));
Manual encapsulation map
function myMap(arr, callback) {
let result = [];
for (let i = 0; i < arr.length; i++) {
result.push(callback(arr[i], i));
}
return result;
}
let res = myMap([1, 2, 3], function(ele, index) {
// console.log(ele,index)
return ele;
});
Mount prototype
Array.prototype.myMap = function(callback) {
let result = [];
for (let i = 0; i < this.length; i++) {
result.push(callback(this[i], i));
}
return result;
};
let res= [1,2,3].myMap((ele)=>ele*2)
边栏推荐
- [FPGA chaos] implementation of FPGA based chaotic system Verilog
- 根据变换矩阵进行图像拼接
- Community Conference | the mosn community will release version 1.0 and promote the evolution of the next generation architecture
- Simple database connection example
- vim命令大全
- fastjson开启safeMode,关闭autoType,去除安全漏洞
- In 2022, don't you know the difference between arrow function and ordinary function?
- Esp32c3 remote serial port
- How to modify the result name of MySQL query result 1 and result 2
- Goodbye to excel and PPT, I found a better visual fool software
猜你喜欢

顺序表与链表---初阶

Function realization and application of trait

【FPGA混沌】基于FPGA的混沌系统verilog实现

云原生概述
![[MySQL] MySQL installation](/img/f0/2b65b71e79ea0cc0790e4848092374.png)
[MySQL] MySQL installation
![What is a request response pair called? [repeat] - what is a request response pair called? [duplicate]](/img/65/a214d137e230b1a1190feb03660f2c.jpg)
What is a request response pair called? [repeat] - what is a request response pair called? [duplicate]

Emperor Wu of Wei knew that he could not correct it, so he stopped offering his words

LINQ group by and select series - LINQ group by and select collection

基于SSH实现健身俱乐部管理系统

路灯照明物联网技术方案,ESP32-S3芯片通信应用,智能WiFi远程控制
随机推荐
PostgreSQL basic introduction and deployment
In 2022, don't you know the difference between arrow function and ordinary function?
Hacker + marathon =? Hacker marathon?
Summary of sequential, modulelist, and moduledict usage in pytorch
Brief introduction to 44 official cases of vrtk3.3 (combined with steamvr)
Kotlin 启动协程、launch 与async的区别、按照顺序启动协程
Unity脚本出现missing时的解决方法
Sed command
19.tornado项目之优化数据库查询
Dynamic gauge (15) - Minimum toll
Hudi of data Lake (14): basic concepts of Apache Hudi
动规(14)-三角形最佳路径问题
Smart panel WiFi linkage technology, esp32 wireless chip module, Internet of things WiFi communication application
Database selected 60 interview questions
云原生概述
Recommended system cleaning tools, cocktail Download
mysql/oracle 以唯一时间为分界,小于等于该时间求和,大于该时间求和
动规(15)-最低通行费
Mosaïque d'images basée sur la matrice de transformation
Network tester operation manual renix rack management