当前位置:网站首页>Slice function of JS handwriting function (thoroughly understand the header but not the footer)
Slice function of JS handwriting function (thoroughly understand the header but not the footer)
2022-07-28 08:40:00 【I haven't figured out the name yet*】
One 、slice Function introduction
- slice The function is used to intercept arrays and strings
- Parameters :start、end
- start The parameter is the interception start position ,end Is the end position of interception . Both parameters are optional .
- When both parameters are not passed , Intercept all
- When only one is passed start when , from start From beginning to end
- Parameters can also be negative , Many people on the Internet say counting from the back , The most correct method is negative + length , Make it a positive number .
Two 、 Handwriting slice
- Array slice
Array.prototype.myslice = function(start,end){
// Judge whether the parameter is negative
if(start&&end){
start = start<0?start = this.length+start:start
end = end<0?end = this.length+end:end
}
if(!start) start = 0
if(!end) end = this.length Judge whether the parameter is transmitted
let res=[];
for(let i=start;i<end;i++){
res.push(this[i])
}
return res
}
const arr = [1,2,3,4,5,6,7,8,9]
console.log(arr.myslice(4,7))
console.log(arr.myslice(-4,-2))
console.log(arr.myslice(2))
- character string slice
With array slice Almost the same
String.prototype.myslice = function(start,end){
if(start&&end){
start = start<0?start = this.length+start:start
end = end<0?end = this.length+end:end
}
if(!start) start = 0
if(!end) end = this.length
let res =''
for(let i=start;i<end;i++){
res +=this[i]
}
return res
}
const str = 'hello javascript'
console.log(str.myslice(4,9))
console.log(str.myslice(-6,-3))
console.log(str.myslice(3))
3、 ... and 、 summary
slice Usage rule
1、 When both parameters are positive : The head is not the tail
2、 When both parameters are negative : Two parameters plus length Become a positive number , No matter how long it takes, no matter how long it takes .( Another way is to wrap the tail without wrapping the head )
3、 When two parameters are positive and negative , Better add length It is easiest to judge when it becomes positive .
- train
const str = 'hello javascript'
console.log(str.myslice(-4,9))
console.log(str.myslice(-6,-3))
console.log(str.myslice(-2))
边栏推荐
- 2022 Niuke multi school second problem solving Report
- How to build the protection system of class protection technology of 2022 series of ISO compliance (Part I)
- 2022牛客多校第二场解题报告
- uniapp的swiper动态设置current值不生效解决办法
- See how Google uses pre training weights in target detection tasks | CVPR 2022
- Day112. Shangyitong: Mobile verification code login function
- CAT1 4G+以太网开发板232数据通过4G模块TCP发到服务器
- Window 2 - > toolbar (28-1)
- 优炫数据库导入和导出方法
- Opencv+paddle Orc recognize pictures and extract table information
猜你喜欢

See how Google uses pre training weights in target detection tasks | CVPR 2022

49-OpenCv深入分析轮廓

解决:IndexError: index 13 is out of bounds for dimension 0 with size 13

uniapp---- 获取当前位置的经纬度等信息的详细步骤(包含小程序)

The fourth phase (2021-2022) research on the implementation of cloud native technology in traditional industries - central state-owned enterprises was officially released

Uniapp ---- detailed steps to obtain the longitude and latitude of the current position and other information (including applet)

ciou损失

jquey的基础语法

数字签名和CA证书

HCIP第九天_BGP实验
随机推荐
Blog Building 9: add search function to Hugo
oracle sql 问题
博客搭建七:hugo
The fourth phase (2021-2022) research on the implementation of cloud native technology in traditional industries - central state-owned enterprises was officially released
C#,入门教程——程序运行时的调试技巧与逻辑错误探针技术与源代码
PostgreSQL:无法更改视图或规则使用的列的类型
Sparksql and flinksql create and link table records
Introduction to self drive tour of snow mountains in the West in January 2018
5张图告诉你:同样是职场人,差距怎么这么大?
[reprint] man Rsync translation (Chinese Manual of Rsync command)
SQL注入 ----前置基础
feign 调用
【OpenCV】生成透明的PNG图像
leetcode/排序数组中两个数字之和
微服务架构 Sentinel 的服务限流及熔断
Opencv+paddle Orc recognize pictures and extract table information
Redis 基本知识,快来回顾一下
[Qt5] small software with 5 people randomly selected from the bid evaluation expert base
单片机IO口控制12V电压通断,MOS和三极管电路
Window 2 - > toolbar (28-1)