![]() Slice counts postive index (from 0) from left to right. Slice will convert it to its positive index. What happens when we pass a negative index? It will start at index 1 (66) and stop at index 2 (99). If we make the end index to be 3: const brr = const marr = brr.slice(1, 3) The element at index 2 (99) won’t be included. This will start from index 1, element 66 and stop at index 1. const brr = const marr = brr.slice(1, 2) l(brr) If we specify an end index, slice will cut from the start index and stop at the index next to meet the end index. So it will start from element 66, which is at index 1, to the end of the array at element 44. const brr = const marr = brr.slice(1) l(marr) Slice cuts from the index specified by start. Start the beginning of the specified portion of the array.Įnd is the end of the specified portion of the array ![]() Slice returns a section of an array (method) Array.slice(start? : number, end?: number): number Also, it mutates the original array, here arr data structure was changed.Īnother test: const arr = const spliceArr = arr.splice(1, 2) l(arr) l(spliceArr) Same result with the original splice implementation. Let’s test our implementation: const arr = const spliceArr = arr.splice(1, 2, 0, 1) l(arr) l(spliceArr) This will cut the arr from element 66 to element 44, then will delete two elements from the extracted array. It(splice) will cut off the specified deleteCount from the array and stuff back the remaining back to the original array.Įxample: const arr = const spliceArr = arr.splice(1,2) So we know that splice is a mutating method. It’s data structure/info/composition has been changed. The result $ node array arr: 56,66 spliceArr: 99,44 false const arr = const spliceArr = arr.splice(2) l("arr: " + arr, "spliceArr: " + spliceArr) l(arr = spliceArr) Is splice a mutating method or non-mutating? Let’s find out. ![]() The result: $ node array arr: spliceArr: Splice: Mutability If we run it: const arr = const spliceArr = arr.splice(1, 3, 0, 1) l("arr: ", arr) l("spliceArr: ", spliceArr) While the result spliceArr will hold the result of splice(1,3) spliceArr = Then 0 and 1 will be added to it.: arr = The remaining argument in the splice call: 0, 1 will be added to the space deleted from the arr array.įirst, splice(1, 3) will make array arr to be: arr = This will cut the array from index 1 to index 3. const arr = const spliceArr = arr.splice(1, 3, 0, 1) This third argument holds the elements to add in the place of the deleted elements. Yes, splice allows us to do just that by specifying a third optional argument. The 2 we passed to splice made it splice two elements, if we had passed 3, it will cut three elements from index 1.Ĭan we insert elements inside the deleted elements? This will start from index 1 ie element 66, and cut two elements towards right: |->-| index => 0 1 2 3 const arr = We will pass a second param to splice to set the number of elements to splice from an array. Setting the number of elements to remove from the spliced array So, arr.splice(-5) will return the whole array: The negative indices on arr start from -1,-2, -3, -4. This is a negative index but not in range of arr negative indices it has. Passing negatives indices will return the whole array too. ![]() When passed to splice, it will return the whole array. It only has index 0, 1, 2, 3.Īny reference after 3 will return undefined. What happens if we pass start index that doesn’t exist?Įxample in our arr array: const arr = Īnd we want to slice from index 5. It will start from element 56 to element 44: So, splice(-3) will start from element 66 to element 44: See the index doesn’t start from 0 but from -1 down. Splice will start from right down: |-> index => -4 -3 -2 -1 const arr = We passed a negative index to splice -3 here. Index starts from left to right, but passing a negative index will make splice start from the right to left.Įxample: const arr = l(arr.splice(-3)) Splice will reverse the order of index numbering. ![]() What happens if we want to start from a negative index? arr.splice(3) |-> index => 0 1 2 3 const arr = Splicing from index 3 will start from 44 to the end of the array. ![]()
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |