当前位置:网站首页>Realize a simple version of array by yourself from
Realize a simple version of array by yourself from
2022-07-07 12:30:00 【Xiaoding Chong duck!】
purpose :
Array.from Method from an array like or iteratable object to create a new , Array instance of shallow copy . ——MDN
See Array.from()
Use the syntax :
Array.from(arrayLike[, mapFn[, thisArg]])
Code :
function myArrayFrom() {
let arrayLike = arguments[0];
// Judge whether the first parameter is empty
if (arrayLike == null) {
throw new TypeError("Array.from requires an array-like object - not null or undefined");
}
let fn = arguments.lenght > 1 ? arguments[1] : undefined;
// Judge whether the second parameter is a function
if (fn && !(typeof fn === 'function')) {
throw new TypeError('Array.from: when provided, the second argument must be a function');
}
let thisArg = arguments.lenght > 2 ? arguments[2] : undefined;
let len = arrayLike.length;
let i = 0, value;
let arr = new Array(len);
while (i < len) {
value = arrayLike[i];
if (fn) {
arr[i] = !thisArg ? fn(value, i) : fn.call(thisArg, value, i);
} else {
arr[i] = value;
}
i++;
}
return arr;
}
verification :
function mySum () {
console.log(arguments);
console.log(myArrayFrom(arguments));
}
mySum(1,2,3,4,42,4,24);
/*
[Arguments] {
'0': 1,
'1': 2,
'2': 3,
'3': 4,
'4': 42,
'5': 4,
'6': 24
}
*/
/*
[
1, 2, 3, 4,
42, 4, 24
]
*/
边栏推荐
- 数据库系统原理与应用教程(010)—— 概念模型与数据模型练习题
- ENSP MPLS layer 3 dedicated line
- JS to convert array to tree data
- SQL lab 11~20 summary (subsequent continuous update) contains the solution that Firefox can't catch local packages after 18 levels
- Sonar:cognitive complexity
- @What happens if bean and @component are used on the same class?
- 【PyTorch实战】用RNN写诗
- 2022 8th "certification Cup" China University risk management and control ability challenge
- Typescript interface inheritance
- 平安证券手机行开户安全吗?
猜你喜欢
[pytorch practice] image description -- let neural network read pictures and tell stories
H3C HCl MPLS layer 2 dedicated line experiment
爱可可AI前沿推介(7.7)
Vxlan 静态集中网关
Financial data acquisition (III) when a crawler encounters a web page that needs to scroll with the mouse wheel to refresh the data (nanny level tutorial)
AirServer自动接收多画面投屏或者跨设备投屏
MPLS experiment
【玩转 RT-Thread】 RT-Thread Studio —— 按键控制电机正反转、蜂鸣器
Ctfhub -web SSRF summary (excluding fastcgi and redI) super detailed
Pule frog small 5D movie equipment | 5D movie dynamic movie experience hall | VR scenic area cinema equipment
随机推荐
Utiliser la pile pour convertir le binaire en décimal
【PyTorch实战】用PyTorch实现基于神经网络的图像风格迁移
wallys/Qualcomm IPQ8072A networking SBC supports dual 10GbE, WiFi 6
Simple network configuration for equipment management
Completion report of communication software development and Application
OSPF exercise Report
Attack and defense world - PWN learning notes
@What happens if bean and @component are used on the same class?
Hi3516全系统类型烧录教程
The road to success in R & D efficiency of 1000 person Internet companies
【统计学习方法】学习笔记——支持向量机(上)
Zero shot, one shot and few shot
SQL blind injection (WEB penetration)
idm服务器响应显示您没有权限下载解决教程
免备案服务器会影响网站排名和权重吗?
Let digital manage inventory
Tutorial on principles and applications of database system (010) -- exercises of conceptual model and data model
What is an esp/msr partition and how to create an esp/msr partition
数据库系统原理与应用教程(009)—— 概念模型与数据模型
Completion report of communication software development and Application