当前位置:网站首页>Introduction to the use of splice() method
Introduction to the use of splice() method
2022-06-24 10:37:00 【Yansuizi blog】
Catalog
2. add to : You can add any item to the specified location
4. It is also possible not to receive a return value
Let's start with an array
var str = ["red","yellow","black","lime","pink","gary"];1. Delete any number of items
Just pass in two parameters . The location of the first item to delete and the number of items to delete
var con = str.splice(1,1); // Delete the second item
console.log(str); //["red", "black", "lime", "pink", "gary"]
console.log(con); //["yellow"]2. add to : You can add any item to the specified location
You only need to provide three parameters : The starting position ,0( Number of items to delete ) And the items to add . If you want to add multiple items, you can continue to write parameters after them, separated by commas .
var con = str.splice(1,0,"orange","blue"); // From the position 1 Start push 1 term
console.log(str); // ["red", "orange", "blue", "black", "lime", "pink", "gary"]
console.log(con); // []3. Replace ( Delete and add ): You can add any item to the specified location , Delete any number of items at the same time .
You need to specify three parameters : The starting position , Number of items deleted and items to be added , The number of items added should not be consistent with the number of items deleted .
var con = str.splice(1,2,"blue"); // Delete the second item Then push... In the deleted position 1 term
console.log(str); // ["red", "blue", "black", "lime", "pink", "gary"]
console.log(con); // ["orange", "blue"]4. It is also possible not to receive a return value
str.splice(0,3,"red");
console.log(str); // ["red", "lime", "pink", "gary"]summary :
1.splice() Method always returns an array , The array contains items that were removed from the original array , If nothing is deleted , Then an empty array will be returned .
2. Be careful The method and slice() It's different ,splice() Will modify the items in the original array .
3. If the parameter is a negative number, output the original array , No operation .
边栏推荐
- Uniapp implementation forbids video drag fast forward
- Leetcode-929: unique email address
- Leetcode-498: diagonal traversal
- How can I solve the problem that the swiper animation animation fails when switching between left and right rotations of the swiper?
- numpy. logical_ or
- JMeter interface test tool foundation - sampler (II)
- 常用的第三方ui框架
- 希尔排序图文详解+代码实现
- leetCode-498: 对角线遍历
- HBuilder制作英雄皮肤抽奖小游戏
猜你喜欢

【资源分享】2022年环境工程与生物技术国际会议(CoEEB 2022)

5. dish management business development

分布式事务原理以及解决分布式事务方案

26. delete duplicates of ordered array

线程的 sleep() 方法与 wait() 方法的区别

1. project environment construction

Stack Title: exclusive time of function

26.删除有序数组的重复项

Leetcode-223: rectangular area

Quick completion guide for mechanical arm (zero): main contents and analysis methods of the guide
随机推荐
Thread pool execution process
线程运行原理
leetCode-1823: 找出游戏的获胜者
2022全网最全最细的jmeter接口测试教程以及接口测试流程详解— JMeter测试计划元件(线程<用户>)
Web project deployment
Leetcode-223: rectangular area
Quick completion guide for mechanical arm (zero): main contents and analysis methods of the guide
Appium automation test foundation - mobile end test environment construction (I)
Resolved: methods with the same name as their class will not be constructors in
Practice sharing of packet capturing tool Charles
leetCode-498: 對角線遍曆
【IEEE出版】2022年智能交通与未来出行国际会议(CSTFM 2022)
牛客-TOP101-BM28
【资源分享】2022年第五届土木,建筑与环境工程国际会议(ICCAEE 2022)
pycharm快捷键大全
23. Opencv——图像拼接项目
希尔排序图文详解+代码实现
机械臂速成小指南(三):机械臂的机械结构
np. float32()
cuda runtime error (801) : Raw out