当前位置:网站首页>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.如果参数写的是负数那么把原本的数组输出出来,不做操作。
边栏推荐
- 2022年能源与环境工程国际研讨会(CoEEE 2022)
- 机械臂速成小指南(零):指南主要内容及分析方法
- 2022 International Symposium on intelligent robots and systems (isoirs 2022)
- 6. package management business development
- Appium自动化测试基础 — 移动端测试环境搭建(一)
- tf. contrib. layers. batch_ norm
- Sort out interface performance optimization skills and kill slow code
- 分布式事务原理以及解决分布式事务方案
- [IEEE publication] 2022 International Conference on industrial automation, robotics and Control Engineering (iarce 2022)
- Quick completion guide for mechanical arm (II): application of mechanical arm
猜你喜欢
随机推荐
机械臂速成小指南(二):机械臂的应用
Quick completion guide for manipulator (III): mechanical structure of manipulator
Leetcode-1051: height checker
Appium automation test foundation - mobile end test environment construction (I)
Six states of threads
HBuilder制作英雄皮肤抽奖小游戏
线程运行原理
【Energy Reports期刊发表】2022年能源与环境工程国际会议(CFEEE 2022)
Wechat applet rich text picture width height adaptive method introduction (rich text)
[IEEE publication] 2022 International Conference on industrial automation, robotics and Control Engineering (iarce 2022)
包装类型与基本类型的区别
Web项目部署
126. 单词接龙 II BFS
tf. contrib. layers. batch_ norm
Uniapp develops a wechat applet to display the map function, and click it to open Gaode or Tencent map.
Flink checkpoint and savepoint
【资源分享】2022年环境工程与生物技术国际会议(CoEEB 2022)
SSM整合
126. word Solitaire II BFS
Leetcode interview question 01.05: primary editing









