当前位置:网站首页>The difference between slice() and slice()
The difference between slice() and slice()
2022-07-23 11:32:00 【Superman can't fly~~】
Tips : When the article is finished , Directories can be generated automatically , How to generate it, please refer to the help document on the right
List of articles
One 、slice
slice(start,end): Method returns the selected element from an existing array , Returns a new array , Contains from start To end( Does not contain the element ) Array elements of .
Be careful : This method does not change the original array , Instead, it returns a sub array , If you want to delete an element in an array , You should use Array.splice() Method .
- start Parameters : must , Specify where to start selecting , If it's negative , Specify the position from the end of the array ,-1 Refers to the last element .
- end Parameters : Optional ( If the parameter is not specified , So the sharded array contains from start The end of all elements of the inverted array , If this parameter is negative , So the rule is the element counting from the end of the array ).
var arr = [1,2,3,4,5];
console.log(arr.slice(1));//[2,3,4,5] Select the serial number from 1 To a new array of all the last elements .
console.log(arr.slice(1,3))//[2,3] It doesn't contain end, Serial number for 3 The elements of
meanwhile slice(start,end) Can act on the cutting of string
/* String cutting and extraction ----slice(start,end),substring(),substr()*/
var str = "Hello,world!";
var sliceStr = str.slice(1,5);//ello ( It doesn't contain end)
var subStr = str.substring(1,5);//ello
var subStr = str.substr(1,5);//ello,
var str = "1000000";
var sliceStr = str.slice(-3);//000 From serial number to -3 To the end
2.splice
2.splice(): This method adds or removes items to or from the array , Returns the deleted item .( This method will change the original array )
splice(index,howmany,item1,…itemX)
- index Parameters : must , Integers , Specify where to add or delete , Use negative numbers , Specify the position from the end of the array .
- howmany Parameters : must , Quantity to delete , If 0, The item is not deleted .
- tem1,…itemX Parameters : Optional , New items added to array .
var arr = [1,2,3,4,5];
console.log(arr.splice(2,1,"hello"));//[3] New array returned
console.log(arr);//[1, 2, "hello", 4, 5] Changed the original array
边栏推荐
- 数字藏品系统开发:NFT的主要特点有哪些?
- Rogue goto statements in C language
- 如何自定义Jsp标签
- Dynamically set the theme color of the card
- General Query & paging code
- [C language] what is a function? Classification and emphasis of functions (help you quickly classify and remember functions)
- phxpaxos安装编译流程
- MySQL索引&&执行计划
- Principle of file upload vulnerability
- JS call, apply, bind
猜你喜欢

How to customize JSP Tags

Security problems of FileInfo in PHP file upload

CTF web common software installation and environment construction

The tree form based on El table and JS xlsx realize the function of downloading excel (II)

C语言调试常见错误——简答

NFT数字藏品平台开发搭建,源码开发数字藏品

Points for attention when using El table to lazy load tree tables

Custom MVC (Part 2)

数字藏品开发/元宇宙数字藏品开发

文件上传漏洞常见绕过方式
随机推荐
Setting optimization and use of MySQL and SQL Server
xtu-ctf Challenges-Reverse 1、2
Upload lab level 1-4
通用查询&分页代码
NepCTF2022 Writeup
Federal Reserve governor Waller: decentralized finance may eventually change the traditional financial market
高阶函数的应用:手写Promise源码(三)
Application of higher-order functions: handwritten promise source code (I)
C语言中的分支和循环语句归属
渗透测试基础
How pycharm packages OCR correctly and makes the packaged exe as small as possible
my_ Implementation of strcpy (classic, simple, practical, collection)
页面实现 “实时数据响应” 的注意事项
数字藏品系统开发:NFT的主要特点有哪些?
Fed Brad: last week's employment report showed that the U.S. economy is robust and can withstand higher interest rates
Composants web - cycle de vie des éléments personnalisés
Solve the problem that the time format of manually querying Oracle database is incorrect (date type)
Framework introduction Mvt
数字藏品系统开发/数字藏品方案介绍
DVWA学习笔记