当前位置:网站首页>记录: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
边栏推荐
- Sword finger offer Day24 math (medium)
- Brief description of CPU
- 常用正则表达式
- [appium] encountered the problem of switching the H5 page embedded in the mobile phone during the test
- 利用Js实现一个千分位
- Compiling principle questions - with answers
- npm安装yarn
- mysql查询字符串类型的字段使用数字类型查询时问题
- NFT ecological development of Ignis public chain: unicorn Donation and development of Art
- ApplicationReadyEvent的使用
猜你喜欢
欧圣电气深交所上市:市值52亿 陆为东父女为美国籍
396. Rotate Function
分布式消息中间件框架选型-数字化架构设计(7)
MySQL数据库中delete、truncate、drop原理详解
ansible自动化运维详解(一)ansible的安装部署、参数使用、清单管理、配置文件参数及用户级ansible操作环境构建
LeetCode简单题之计算字符串的数字和
青苹果影视系统源码 影视聚合 影视导航 影视点播网站源码
idea:使用easyYapi插件导出yapi接口
Briefly describe the hierarchical strategy of memory
【学习】从零开始的音视频开发(9)——NuPlayer
随机推荐
CSV column extract column extraction
Campus transfer second-hand market source code download
关于ORB——SLAM运行中关键帧位置越来越近的异常说明
Qt编译QtXlsx库
The annotation is self-defined by implementing the parameter parser handlermethodargumentresolver interface
岛屿的个数
分布式消息中间件框架选型-数字化架构设计(7)
Situational leaders - Chapter 7, solving performance problems
【学习】从零开始的音视频开发(9)——NuPlayer
编译原理题-带答案
AAAI 2022 recruit speakers!!
怎么读书读论文
Idea: export Yapi interface using easyyapi plug-in
【解释】get ORA-12838: cannot read/modify an object after modifying it in parallel
作文以记之 ~ 二叉树的后序遍历
Somme numérique de la chaîne de calcul pour un problème simple de leetcode
校园转转二手市场源码下载
An idea plug-in that doesn't work, but can install X
跨域配置报错: When allowCredentials is true, allowedOrigins cannot contain the special value “*“
将实例化对象的方法 给新的对象用