当前位置:网站首页>记录:js删除数组中某一项或几项的几种方法
记录:js删除数组中某一项或几项的几种方法
2022-04-23 07:34:00 【beinlife】
1:js中的splice方法
splice(index,len,[item]) 注释:该方法会改变原始数组。
splice有3个参数,它也可以用来替换/删除/添加数组内某一个或者几个值
index:数组开始下标 len: 替换/删除的长度 item:替换的值,删除操作的话 item为空
如:arr = ['a','b','c','d']
删除 ---- item不设置
arr.splice(1,1) //['a','c','d'] 删除起始下标为1,长度为1的一个值,len设置的1,如果为0,则数组不变
arr.splice(1,2) //['a','d'] 删除起始下标为1,长度为2的一个值,len设置的2
替换 ---- item为替换的值
arr.splice(1,1,'ttt') //['a','ttt','c','d'] 替换起始下标为1,长度为1的一个值为‘ttt’,len设置的1
arr.splice(1,2,'ttt') //['a','ttt','d'] 替换起始下标为1,长度为2的两个值为‘ttt’,len设置的1
添加 ---- len设置为0,item为添加的值
arr.splice(1,0,'ttt') //['a','ttt','b','c','d'] 表示在下标为1处添加一项‘ttt’
看来还是splice最方便啦
2:delete delete删除掉数组中的元素后,会把该下标出的值置为undefined,数组的长度不会变
如:delete arr[1] //['a', ,'c','d'] 中间出现两个逗号,数组长度不变,有一项为undefined
版权声明
本文为[beinlife]所创,转载请带上原文链接,感谢
https://blog.csdn.net/beinlife/article/details/52987315
边栏推荐
猜你喜欢
Qtablewidget header customization and beautification developed by pyqt5 (with source code download)
欧圣电气深交所上市:市值52亿 陆为东父女为美国籍
程序,进程,线程;内存结构图;线程的创建和启动;Thread的常用方法
Description of the abnormity that the key frame is getting closer and closer in the operation of orb slam
vslam PPT
扎心了!一女子发朋友圈羡慕别人按时发工资被开除,连点赞的同事也一同被开除了...
One click cleanup of pycharm and jupyter cache files under the project
怎么读书读论文
一款拥有漂亮外表的Typecho简洁主题_Scarfskin 源码下载
An article understands variable lifting
随机推荐
colorui 解决底部导航遮挡内容问题
LeetCode中等题之旋转函数
A simple theme of Typecho with beautiful appearance_ Scarfskin source code download
Install MySQL for Ubuntu and query the average score
Flink SQL实现流批一体
clang 如何产生汇编文件
万物互联下如何对设备进行加密
編譯原理題-帶答案
Data deletion and modification (MySQL)
Data security has become a hidden danger. Let's see how vivo can make "user data" armor again
Multi vision slam
Qt读写XML文件
跨域配置报错: When allowCredentials is true, allowedOrigins cannot contain the special value “*“
总线结构概述
dried food! Point based: differentiable Poisson solver
An article understands variable lifting
Campus transfer second-hand market source code download
一款拥有漂亮外表的Typecho简洁主题_Scarfskin 源码下载
QFileDialog select multiple files or folders
LeetCode15. Sum of three