当前位置:网站首页>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
]
*/
边栏推荐
- 浅谈估值模型 (二): PE指标II——PE Band
- H3C HCl MPLS layer 2 dedicated line experiment
- How much does it cost to develop a small program mall?
- On valuation model (II): PE index II - PE band
- leetcode刷题:二叉树23(二叉搜索树中的众数)
- IPv6 experiment
- Idea 2021 Chinese garbled code
- About web content security policy directive some test cases specified through meta elements
- 2022-07-07日报:GAN发明者Ian Goodfellow正式加入DeepMind
- Sonar:cognitive complexity
猜你喜欢
![Routing strategy of multi-point republication [Huawei]](/img/5c/2e3b739ce7199f0d2a4ddd7c3856fc.jpg)
Routing strategy of multi-point republication [Huawei]

(to be deleted later) yyds, paid academic resources, please keep a low profile!

Zhimei creative website exercise

About sqli lab less-15 using or instead of and parsing
![[pytorch practice] image description -- let neural network read pictures and tell stories](/img/39/b2c61ae0668507f50426b01f2deee4.png)
[pytorch practice] image description -- let neural network read pictures and tell stories

Dialogue with Wang Wenyu, co-founder of ppio: integrate edge computing resources and explore more audio and video service scenarios

The left-hand side of an assignment expression may not be an optional property access. ts(2779)

静态Vxlan 配置

Configure an encrypted web server

SQL Lab (32~35) contains the principle understanding and precautions of wide byte injection (continuously updated later)
随机推荐
【PyTorch实战】用PyTorch实现基于神经网络的图像风格迁移
"Series after reading" my God! It's so simple to understand throttling and anti shake~
The left-hand side of an assignment expression may not be an optional property access.ts(2779)
【PyTorch实战】用RNN写诗
30. Feed shot named entity recognition with self describing networks reading notes
sql-lab (54-65)
Routing strategy of multi-point republication [Huawei]
Inverted index of ES underlying principle
An error occurred when vscade tried to create a file in the target directory: access denied [resolved]
Error in compiling libssl
利用栈来实现二进制转化为十进制
跨域问题解决方案
Apache installation problem: configure: error: APR not found Please read the documentation
ENSP MPLS layer 3 dedicated line
JS to convert array to tree data
(to be deleted later) yyds, paid academic resources, please keep a low profile!
Problem: the string and characters are typed successively, and the results conflict
爱可可AI前沿推介(7.7)
About web content security policy directive some test cases specified through meta elements
DOM parsing XML error: content is not allowed in Prolog