当前位置:网站首页>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)
边栏推荐
- PostMessage implements window communication
- webpack---优化_缓存
- MySQL创建用户并授权
- Object detection model rfbnet -- a very useful model
- Solution en cas de défaillance du script Unity
- 电商中台系统架构
- 消息队列概述
- Review of technical economy and Enterprise Management Chapter 4
- Pat class B 1067 trial password (20 points)
- Image mosaic based on transformation matrix
猜你喜欢

【FPGA+FFT】基于FPGA的FFT频率计设计与实现

MySQL的check约束数字问题

Smart Panel wifi Linkage Technology, esp32 wireless chip module, Internet of Things WiFi Communication Application

Paper recommendation: relicv2, can the new self supervised learning surpass supervised learning on RESNET?

The memory four area model of C language program

Review of technical economy and Enterprise Management Chapter 4

KV storage separation principle and performance evaluation of nebula graph

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

根据变换矩阵进行图像拼接

Function realization and application of trait
随机推荐
认真工作对自己到底意味着什么?
php和js去掉所有空格
Implementation of fitness club management system based on SSH
分布式计算的八大谬论
Is it safe for Guojin Securities Commission Jinbao to open an account? How should we choose securities companies?
Pat class B 1067 trial password (20 points)
SqEL简单上手
Smart Panel wifi Linkage Technology, esp32 wireless chip module, Internet of Things WiFi Communication Application
后续版本是否会支持代码块搜索高亮显示
Simple database connection example
Concept and introduction of microservice
成功解决:TypeError: the JSON object must be str, bytes or bytearray, not dict
[C language] dynamic memory allocation
oralce 处理列转行的三种方式 最后生成表格样式数据
关于 国产麒麟Qt编译报错“xxx.pri has modification time xxxx s in the futrue“ 的解决方法
Create a new table in the database. There was no problem before. Today
魏武帝 太祖知不可匡正,遂不复献言
R language uses the coxph function of survival package to build Cox regression model, uses the ggrisk function of ggrisk package to visualize the risk score map (risk score map) of Cox regression, and
Eight fallacies of distributed computing
Dynamic gauge (15) - Minimum toll