当前位置:网站首页>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 .
边栏推荐
- Quick completion guide for mechanical arm (I): development overview of mechanical arm
- 283. move zero
- Machine learning - principal component analysis (PCA)
- JMeter接口测试工具基础 — Badboy工具
- 线程运行原理
- What you must know about distributed systems -cap
- Status of the thread pool
- 牛客-TOP101-BM28
- 栈题目:函数的独占时间
- Resolved: methods with the same name as their class will not be constructors in
猜你喜欢

Appium自动化测试基础 — 移动端测试环境搭建(一)

2022全网最全最细的jmeter接口测试教程以及接口测试流程详解— JMeter测试计划元件(线程<用户>)

Petit guide de construction rapide du bras mécanique (II): application du bras mécanique

2022 the most complete and detailed JMeter interface test tutorial and detailed interface test process in the whole network - JMeter test plan component (thread < user >)

Quick completion guide for mechanical arm (I): development overview of mechanical arm

Leetcode-223: rectangular area

283.移动零

3.员工的增删改查

Quick completion guide for manipulator (III): mechanical structure of manipulator

leetCode-498: 对角线遍历
随机推荐
Multithreaded applications - improve efficiency
Stack Title: fractions in parentheses
[ei sharing] the 6th International Conference on ship, ocean and Maritime Engineering in 2022 (naome 2022)
[IEEE publication] 2022 International Conference on service robots (iwosr 2022)
JMeter interface test tool foundation - sampler (II)
283. move zero
Image click enlargement and adaptive size in the applet rich text
Learn to use PHP to implement unlimited comments and unlimited to secondary comments solutions
How to customize sharing links in wechat for H5 web pages
Flink集群搭建以及企业级yarn集群搭建
Thread pool execution process
88. merge ordered arrays
栈题目:函数的独占时间
抓包工具charles實踐分享
【资源分享】2022年环境工程与生物技术国际会议(CoEEB 2022)
2. login and exit function development
Uniapp implements the function of clicking to make a call
Web项目部署
pycharm快捷键大全
Machine learning perceptron and k-nearest neighbor