当前位置:网站首页>(JS)数组方法:slice和splice
(JS)数组方法:slice和splice
2022-06-29 09:57:00 【不愿透露姓名的余菜鸟】
- Array.slice(start,end)
拷贝数组,如果不指定start和end则表示拷贝整个数组,如果只指定start则拷贝从start到数组末尾,如果start和end都指定,则拷贝start到end-1,。此操作不会改变源数组
let arr = [1,2,3,4];
console.log(arr.slice(-1)) //[4]
当start<end时,会返回一个空数组,当start为负数时,从数组末尾开始计算
2. Array.splice(start,count,value)
截取数组,如果只传入start,则表示从start位置一直截取到数组末尾,如果传入start,count则表示从start截取count个数,返回截取的数组,如果传入start,count,value,则表示从start开始,删除count个数,并将value插入。
let arr = [1,2,3,4];
console.log(arr.splice(1)); //[2, 3, 4]
console.log(arr) //[1]
let arr = [1,2,3,4];
console.log(arr.splice(1,1)); //[2]
console.log(arr) //[1, 3, 4]
let arr = [1,2,3,4];
console.log(arr.splice(1,1,6)); //[2]
console.log(arr) //[1, 6, 2, 4]
当start为负数时,start<count时,会返回一个空数组,当start为负数,count为整数,当start和count都为负数时,则count为0,表示在start之前插入value。
边栏推荐
- BUUCTF RE-easyre
- Recommended embedded learning books [easy to understand]
- Getting started with the lvgl Library - Animation
- 如何优雅的写 Controller 层代码?
- 《MongoDB入门教程》第02篇 MongoDB安装
- CLR via C reading notes - loading and AppDomain
- 他98年的,我玩不过他...
- js post下载文件
- Ningde era Kirin battery has greater ambition
- Does anyone encounter this problem when flinkcdc synchronizes MySQL?
猜你喜欢

“AI x 科学计算”进行时,华为昇思 MindSpore 赛题火热开启,等你来!

8年打磨,《游戏设计梦工厂》发布史诗级更新!

全面理解Synchronized

容器平台性能如何测试,稳定性、扩展效率、组件性能

学习通否认 QQ 号被盗与其有关:已报案;iPhone 14 量产工作就绪:四款齐发;简洁优雅的软件早已是明日黄花|极客头条...

UserWarning: Usage of dash-separated ‘script-dir‘ will not be supported in future versions. 笔记

Fully understand the MESI cache consistency protocol

30-year-old female, ordinary software testing Yuanyuan, confused and anxious about her career

ssh密钥泄露(B模块赛题)——应用服务漏洞扫描与利用

《Datawhale推荐系统教程》来了!
随机推荐
1- database understanding
8年打磨,《游戏设计梦工厂》发布史诗级更新!
【C语言进阶】文件操作(一)
Here comes the tutorial of datawhale recommendation system!
USB转RS485串口电路设计「建议收藏」
细粒度识别、分类、检索数据集整理
常见电机分类和驱动原理动画[通俗易懂]
Analysis of reentrantlock source code of AQS
容器平台性能如何测试,稳定性、扩展效率、组件性能
他98年的,我玩不过他...
Luoqingqi: has high-end household appliances become a red sea? Casati took the lead in breaking the game
With this tool, automatic identification and verification code is no longer a problem
[200 opencv routines] 214 Detailed explanation of drawing ellipse parameters
The product strength is not inferior to that of BYD. Geely Dihao l Raytheon hi · x delivered 10000 units in the first month
在 2022 年找工作,毕业生们的 “最后一课”
【C语言进阶】特殊自定义类型
js post下载文件
《如何阅读一本书》读后总结
The difference between & & and &
MySQL查询时如何找出错误格式的手机号