当前位置:网站首页>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
]
*/
边栏推荐
- SQL Lab (32~35) contains the principle understanding and precautions of wide byte injection (continuously updated later)
- leetcode刷题:二叉树25(二叉搜索树的最近公共祖先)
- EPP+DIS学习之路(2)——Blink!闪烁!
- 数据库系统原理与应用教程(007)—— 数据库相关概念
- 2022 8th "certification Cup" China University risk management and control ability challenge
- 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)
- 金融数据获取(三)当爬虫遇上要鼠标滚轮滚动才会刷新数据的网页(保姆级教程)
- [statistical learning methods] learning notes - improvement methods
- 数据库系统原理与应用教程(009)—— 概念模型与数据模型
- wallys/Qualcomm IPQ8072A networking SBC supports dual 10GbE, WiFi 6
猜你喜欢
Learning and using vscode
EPP+DIS学习之路(1)——Hello world!
【统计学习方法】学习笔记——第五章:决策树
Hi3516全系统类型烧录教程
Solutions to cross domain problems
Tutorial on the principle and application of database system (011) -- relational database
Preorder, inorder and postorder traversal of binary tree
消息队列消息丢失和消息重复发送的处理策略
About sqli lab less-15 using or instead of and parsing
[statistical learning methods] learning notes - improvement methods
随机推荐
Tutorial on principles and applications of database system (007) -- related concepts of database
爱可可AI前沿推介(7.7)
跨域问题解决方案
开发一个小程序商城需要多少钱?
111. Network security penetration test - [privilege escalation 9] - [windows 2008 R2 kernel overflow privilege escalation]
JS to convert array to tree data
数据库系统原理与应用教程(010)—— 概念模型与数据模型练习题
SQL head injection -- injection principle and essence
数据库系统原理与应用教程(009)—— 概念模型与数据模型
Sonar:cognitive complexity
【统计学习方法】学习笔记——支持向量机(下)
Preorder, inorder and postorder traversal of binary tree
Baidu digital person Du Xiaoxiao responded to netizens' shouts online to meet the Shanghai college entrance examination English composition
Upgrade from a tool to a solution, and the new site with praise points to new value
平安证券手机行开户安全吗?
RHSA first day operation
SQL injection -- Audit of PHP source code (take SQL lab 1~15 as an example) (super detailed)
金融数据获取(三)当爬虫遇上要鼠标滚轮滚动才会刷新数据的网页(保姆级教程)
SQL lab 26~31 summary (subsequent continuous update) (including parameter pollution explanation)
2022 8th "certification Cup" China University risk management and control ability challenge