当前位置:网站首页>splice()方法的使用介绍
splice()方法的使用介绍
2022-06-24 09:50:00 【燕穗子博客】
目录
3.替换(删除再添加):可以向指定位置添加任意的项,同时删除任意数量的项。
先声明一个数组
var str = ["red","yellow","black","lime","pink","gary"];1.删除任意数量的项
只需要传入两个参数即可。要删除的第一项的位置和要删除的项数
var con = str.splice(1,1); //删除第二项
console.log(str); //["red", "black", "lime", "pink", "gary"]
console.log(con); //["yellow"]2.添加:可以向指定位置添加任意的项
只需要提供三个参数即可:起始位置,0(要删除的项数)和要添加的项。如果要添加多项可以继续在后面写参数用逗号分隔。
var con = str.splice(1,0,"orange","blue"); //从位置1开始推入1项
console.log(str); // ["red", "orange", "blue", "black", "lime", "pink", "gary"]
console.log(con); // []3.替换(删除再添加):可以向指定位置添加任意的项,同时删除任意数量的项。
需要指定三个参数:起始位置,删除的项数和要添加的项数,添加的项数不用和删除的项数保持一致。
var con = str.splice(1,2,"blue"); //删除第二项 然后在删除的位置上推入1项
console.log(str); // ["red", "blue", "black", "lime", "pink", "gary"]
console.log(con); // ["orange", "blue"]4.不接收返回值也是可以的
str.splice(0,3,"red");
console.log(str); // ["red", "lime", "pink", "gary"]总结:
1.splice()方法始终会返回一个数组,该数组中包含从原始数组中删除的项,如果没有删除任何项,那么将会返回一个空数组。
2.注意 该方法和slice()是不一样的,splice()会修改原数组中的项。
3.如果参数写的是负数那么把原本的数组输出出来,不做操作。
边栏推荐
- Illustration miscellaneous [for archiving to prevent loss]
- 5.菜品管理业务开发
- 分布式事务原理以及解决分布式事务方案
- JMeter接口测试工具基础— 取样器sampler(二)
- 机械臂速成小指南(零):指南主要内容及分析方法
- 2022全网最全最细的jmeter接口测试教程以及接口测试流程详解— JMeter测试计划元件(线程<用户>)
- Flink cluster construction and enterprise level yarn cluster construction
- Outils de capture de paquets
- 【资源分享】2022年第五届土木,建筑与环境工程国际会议(ICCAEE 2022)
- Wechat applet rich text picture width height adaptive method introduction (rich text)
猜你喜欢

leetCode-223: 矩形面积
![Illustration miscellaneous [for archiving to prevent loss]](/img/cf/bf48bfda020a3c8690339a388efc5d.png)
Illustration miscellaneous [for archiving to prevent loss]

charles抓包工具使用教程

Solve the timeout of Phoenix query of dbeaver SQL client connection

机械臂速成小指南(一):机械臂发展概况

抓包工具charles實踐分享

SQL Server AVG function rounding

Flink checkPoint和SavePoint

How to customize sharing links in wechat for H5 web pages

2022 International Symposium on intelligent robots and systems (isoirs 2022)
随机推荐
Thread pool execution process
Younger sister Juan takes you to learn JDBC --- 2-day sprint Day1
web网站开发,图片懒加载
[ei sharing] the 6th International Conference on ship, ocean and Maritime Engineering in 2022 (naome 2022)
cuda runtime error (801) : Raw out
Six states of threads
Leetcode interview question 01.05: primary editing
6. package management business development
283.移动零
抓包工具charles實踐分享
Quick completion guide for mechanical arm (II): application of mechanical arm
The difference between static link library and dynamic link library
The difference between the sleep () method and the wait () method of a thread
Illustration miscellaneous [for archiving to prevent loss]
Web project deployment
126. 单词接龙 II BFS
2022全网最全最细的jmeter接口测试教程以及接口测试流程详解— JMeter测试计划元件(线程<用户>)
JMeter接口测试工具基础— 使用Badboy录制JMeter脚本
Role of message queuing
Practice sharing of packet capturing tool Charles