In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces what is the use of splice () and slice () in JavaScript, which can be used for reference. Interested friends can refer to it. I hope you can learn a lot after reading this article.
1 splice () 1.1 description
The splice () method adds / removes items to / from the array, and then returns the deleted items. This method changes the original array. Link
1.2Syntax arrayObject.splice (index,howmany,item1,.,itemX)
Parameters.
Index: required. An integer that specifies the location of the added / deleted items, using negative numbers to specify the location from the end of the array.
Howmany: required. The number of items to delete. If set to 0, the item is not deleted.
Item1,..., itemX: optional. A new item added to the array.
Return value
Array: contains a new array of deleted items, if any.
1.3 example / / add items (function () {var arr = [0,1,2,3,4]; arr.splice (1,0,9,10); / / Index location / number of deletions / optional, add items, multiple console.log (arr); / / [0,9,10,1,2,3,4]} ()) / delete items (function () {var arr = [0,1,2,3,4]; arr.splice (1,2); console.log (arr); / / [0,3,4]} ()); / / delete and add items (function () {var arr = [0,1,2,3,4]; arr.splice (1,2,9,10,11); console.log (arr) / / [0,9,10,11,3,4]} (); 2 slice () 2.1description
The slice () method returns the selected element from an existing array. This method does not modify the array, but returns a subarray.
2.2 Syntax arrayObject.slice (start,end)
Parameters.
Start: required. Specify where to start the selection. If it is negative, it specifies the position from the end of the array. That is,-1 refers to the last element,-2 refers to the penultimate element, and so on.
End: optional. Specifies where to end the selection. This parameter is the array subscript at the end of the array fragment. If this parameter is not specified, the shredded array contains all the elements from start to the end of the array. If this parameter is negative, it specifies the element starting at the end of the array.
Return value
Returns a new array containing elements from the arrayObject from start to end (excluding this element).
2.3 example / / intercept the middle segment (function () {var arr = [0,1,2,3,4]; arrS = arr.slice (1,3); / / from index 1 to before index 3, excluding the item console.log (arrS) of index 3; / / [1,2]} (); / / intercept middle to last (function () {var arr = [0,1,2,3,4]) ArrS = arr.slice (1); / / end is empty, console.log (arrS) from index 1 to end; / / [1,2,3,4]} (); / / get the last item (function () {var arr = [0,1,2,3,4]; arrS = arr.slice (- 1) / /-1 project to the end,-1 is the last one,-2 the penultimate console.log (arrS); / / [4]} (); / / exclude the last item (function () {var arr = [0,1,2,3,4]; arrS = arr.slice (0,-1) / / first to 1st, excluding 1st (last) console.log (arrS); / / [0,1,2,3]} () Thank you for reading this article carefully. I hope the article "what is the use of splice () and slice () in JavaScript" shared by the editor will be helpful to you. At the same time, I also hope you will support us and pay attention to the industry information channel. More related knowledge is waiting for you to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.