当前位置:网站首页>slice()和splice()区别
slice()和splice()区别
2022-07-23 05:39:00 【超人不会飞~~】
提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
一、slice
slice(start,end):方法可从已有数组中返回选定的元素,返回一个新数组,包含从start到end(不包含该元素)的数组元素。
注意:该方法不会改变原数组,而是返回一个子数组,如果想删除数组中的一段元素,应该使用Array.splice()方法。
- start参数:必须,规定从何处开始选取,如果为负数,规定从数组尾部算起的位置,-1是指最后一个元素。
- end参数:可选(如果该参数没有指定,那么切分的数组包含从start倒数组结束的所有元素,如果这个参数为负数,那么规定是从数组尾部开始算起的元素)。
var arr = [1,2,3,4,5];
console.log(arr.slice(1));//[2,3,4,5] 选择序列号从1到最后的所有元素组成的新数组。
console.log(arr.slice(1,3))//[2,3] 不包含end,序列号为3的元素
同时slice(start,end)可以作用于字符串的切割
/*字符串切割和提取----slice(start,end),substring(),substr()*/
var str = "Hello,world!";
var sliceStr = str.slice(1,5);//ello (不包含end)
var subStr = str.substring(1,5);//ello
var subStr = str.substr(1,5);//ello,
var str = "1000000";
var sliceStr = str.slice(-3);//000 从序列号为-3的位置到最后
2.splice
2.splice():该方法向或者从数组中添加或者删除项目,返回被删除的项目。(该方法会改变原数组)
splice(index,howmany,item1,…itemX)
- index参数:必须,整数,规定添加或者删除的位置,使用负数,从数组尾部规定位置。
- howmany参数:必须,要删除的数量,如果为0,则不删除项目。
- tem1,…itemX参数:可选,向数组添加的新项目。
var arr = [1,2,3,4,5];
console.log(arr.splice(2,1,"hello"));//[3] 返回的新数组
console.log(arr);//[1, 2, "hello", 4, 5] 改变了原数组
边栏推荐
- Keras saves the best model in the training process
- Using pytorch to realize the flower recognition classifier based on VGg 19 pre training model, the accuracy reaches 97%
- img标签设置height和width无效
- IMG tag settings height and width are invalid
- Project process summary
- [监控部署实操]基于granfana展示Prometheus的图表和loki+promtail的图表
- 请求数据获取与响应
- Getting started with RPC and thrift
- 混入视图基类
- Markdown common syntax records
猜你喜欢

【C语言】什么是函数?函数的分类和侧重(帮你快速分类和记忆函数)

Machine learning algorithm for large factory interview (6) time series analysis
![[pytho-flask笔记5]蓝图简单使用](/img/0a/00b259f42e2fa83d4871263cc5f184.png)
[pytho-flask笔记5]蓝图简单使用

Five methods to prevent over fitting of neural network

gbk编解码器无法解码的问题,有可能出题出在文件名上

视、音频分开的网站内容如何合并?批量下载代码又该如何编写?

Pywinauto+某应用程序(学习至第9讲)--受阻

JDBC Learning and simple Encapsulation

Pytorch white from zero uses North pointing

安装企业版pycharm以及anaconda
随机推荐
【Pyradiomics】提取的影像组学特征值不正常(很多0和1)
初识Flask
Use of views
大米商城注册
牛客刷题记录--Mysql
wps中的word中公式复制完后是图片
Pywinauto+某应用程序(学习至第9讲)--受阻
视图的使用
Spectral clustering | Laplace matrix
Hyperlink de underlined code
JDBC的学习以及简单封装
View set and route
[Anaconda environmental management and package management]
对NLP中transformer里面decoder的理解
Project process summary
Error handling of "listener not started or database service not registered" in Oracle database creation
D2DEngine食用教程(2)———绘制图像
JWT header进行编码过程
Framework introduction Mvt
Install enterprise pycharm and Anaconda