当前位置:网站首页>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
边栏推荐
- Goodbye if else
- The 6th "Blue Hat Cup" National College Students' Cyber Security Skills Competition - preliminary writeup
- Anti shake and throttling of JS
- Laravel API interface + token authentication login
- D2dengine edible tutorial (2) -- drawing images
- Vite X Figma 打造设计师专属的 i18n 插件
- ETH转账次数达到一个月高点
- Transform: translate (-50%, -50%) border problem
- Customize foreach tags & select tags to echo data
- sql-labs 5-6通关笔记
猜你喜欢
随机推荐
composer的一些操作
The object in $attrs doesn't change, but it triggers watch listening?
DVWA学习笔记
通用查询&分页代码
Burpsuite learning notes
Sqli lab 1-16 notes with customs clearance
Application of higher-order functions: handwritten promise source code (III)
C语言调试常见错误——简答
Custom formula input box
Bank of Indonesia governor said the country is actively exploring encrypted assets
高阶函数的应用:手写Promise源码(一)
Vite X Figma 打造设计师专属的 i18n 插件
[untitled]
ETH转账次数达到一个月高点
Eth transfer times reached a one month high
使用el-table懒加载树形表格时的注意点
General Query & paging code
php+码云 代码钩子自动更新线上代码
手写Promise.resolve,Promise.reject, Promise.all
渗透测试基础









