当前位置:网站首页>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
]
*/
边栏推荐
- <No. 8> 1816. Truncate sentences (simple)
- When OSPF specifies that the connection type is P2P, it enables devices on both ends that are not in the same subnet to Ping each other
- Pule frog small 5D movie equipment | 5D movie dynamic movie experience hall | VR scenic area cinema equipment
- Will the filing free server affect the ranking and weight of the website?
- 爱可可AI前沿推介(7.7)
- Unity 贴图自动匹配材质工具 贴图自动添加到材质球工具 材质球匹配贴图工具 Substance Painter制作的贴图自动匹配材质球工具
- 【统计学习方法】学习笔记——第五章:决策树
- 浅谈估值模型 (二): PE指标II——PE Band
- 静态Vxlan 配置
- Preorder, inorder and postorder traversal of binary tree
猜你喜欢
SQL Lab (46~53) (continuous update later) order by injection
SQL lab 11~20 summary (subsequent continuous update) contains the solution that Firefox can't catch local packages after 18 levels
Zhimei creative website exercise
wallys/Qualcomm IPQ8072A networking SBC supports dual 10GbE, WiFi 6
How to use PS link layer and shortcut keys, and how to do PS layer link
<No. 8> 1816. Truncate sentences (simple)
Processing strategy of message queue message loss and repeated message sending
Unity map auto match material tool map auto add to shader tool shader match map tool map made by substance painter auto match shader tool
【统计学习方法】学习笔记——第五章:决策树
The IDM server response shows that you do not have permission to download the solution tutorial
随机推荐
Unity map auto match material tool map auto add to shader tool shader match map tool map made by substance painter auto match shader tool
leetcode刷题:二叉树27(删除二叉搜索树中的节点)
Realize all, race, allsettled and any of the simple version of promise by yourself
即刻报名|飞桨黑客马拉松第三期盛夏登场,等你挑战
leetcode刷题:二叉树25(二叉搜索树的最近公共祖先)
【统计学习方法】学习笔记——第四章:朴素贝叶斯法
Using stack to convert binary to decimal
Zhimei creative website exercise
【统计学习方法】学习笔记——逻辑斯谛回归和最大熵模型
【PyTorch实战】用PyTorch实现基于神经网络的图像风格迁移
利用栈来实现二进制转化为十进制
Upgrade from a tool to a solution, and the new site with praise points to new value
VSCode的学习使用
Let digital manage inventory
<No. 9> 1805. Number of different integers in the string (simple)
Problem: the string and characters are typed successively, and the results conflict
【玩转 RT-Thread】 RT-Thread Studio —— 按键控制电机正反转、蜂鸣器
百度数字人度晓晓在线回应网友喊话 应战上海高考英语作文
Solve server returns invalid timezone Go to ‘Advanced’ tab and set ‘serverTimezone’ property manually
OSPF exercise Report