当前位置:网站首页>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)
边栏推荐
- Sed command
- Object detection model rfbnet -- a very useful model
- 简单的数据库连接示例
- 【clickhouse专栏】新建库角色用户初始化
- Double objective learning materials sorting
- Page crash handling method
- Drop down menu dropdown yyds dry inventory of semantic UI
- Functions (arguments, formal parameters, bubbling)
- La mise en évidence de la recherche par blocs est - elle prise en charge dans les versions ultérieures?
- Yyds dry inventory MySQL learning - how transactions are isolated
猜你喜欢

数据库精选 60 道面试题

The road of global evolution of vivo global mall -- multilingual solution

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

【clickhouse专栏】新建库角色用户初始化

PyTorch中的Sequential、ModuleList和ModuleDict用法总结

Smart panel WiFi linkage technology, esp32 wireless chip module, Internet of things WiFi communication application

Absolute positioning three ways to center the box

DS18B20 digital thermometer (I) electrical characteristics, power supply and wiring mode

mysql/oracle 以唯一时间为分界,小于等于该时间求和,大于该时间求和

路灯照明物联网技术方案,ESP32-S3芯片通信应用,智能WiFi远程控制
随机推荐
怎样拥有心灵的平和?获得一颗全新的心灵
MySQL创建用户并授权
Is there a row limit for a single MySQL table
Convert py file to EXE file
JSP实现银柜台业务绩效考核系统
Emperor Wu of Wei knew that he could not correct it, so he stopped offering his words
【FPGA混沌】基于FPGA的混沌系统verilog实现
Solution en cas de défaillance du script Unity
Eight fallacies of distributed computing
Function realization and application of trait
SqEL简单上手
PyTorch中的Sequential、ModuleList和ModuleDict用法总结
Mongodb essence summary
Goodbye to excel and PPT, I found a better visual fool software
路灯照明物联网技术方案,ESP32-S3芯片通信应用,智能WiFi远程控制
Kotlin 启动协程、launch 与async的区别、按照顺序启动协程
[MySQL] MySQL installation
Notes relatives à l'application du certificat de l'AC et de la paire de clés
2.28 (defect filling) data type conversion exception handling part multi threading
Evolution and practice of Unicom real-time computing platform