JavaScript Array slice

 JavaScript slice can arbitrarily cut out a part of the  JavaScript Array array , as long as you specify the starting position and ending position of the array element to be intercepted, it is quite practical for occasionally needing to take out a certain section in a large array. In addition, splice can also be used to extract part of a string. The usage is similar to the substring and substr functions, but the special feature is that the slice parameter can use negative numbers and can handle arrays or strings. This topic uses slices to handle arrays. Mainly, you can refer to this article if you want to understand how slices process strings: JavaScript slice . Let's take a look at how slice extracts a part of the array.


JavaScript Array.slice basic syntax

Array.slice( begin, end)


Array is any array that you want to be intercepted. There are two important parameters in the slice parentheses. The first is begin, which represents which element of the array to start from. The first array element is 0, and the second is 1. ... and so on, begin is a required item, and the second parameter end represents the end position. When the slice intercepts the element, it will get to the end and not include the end. If the end is not filled in, the slice will start to capture from begin Get to the last array element. It is quite interesting that both parameters of begin and end can be negative numbers, which means that the calculation starts from the last array element, the second to last array element is -2, the third to last array element is -3... and so on .

JavaScript Array.slice example
<script type="text/javascript">
var NewArray = ["A", "B", "C", "D", "E"];
document.write(NewArray); // output A,B,C ,D,E
document.write( NewArray.slice(2) ); // output C,D,E
document.write( NewArray.slice(1,3) ); // output B,C
document.write( NewArray. slice(-2,-1) ); // output D
document.write(NewArray); // output A,B,C,D,E
</script>

An example of a start, prepare a new array NewArray, through document.write will go through JavaScript slice output to the screen processing results.
  • The result of the first output is the original array.
  • The result of NewArray.slice(2) is output for the second time. Please note that the first array element is counted from 0. Slice(2) means to extract all the array elements after the third array element and return a new The output result is C, D, E.
  • Output the result of NewArray.slice(1,3) for the third time, which means that the extraction starts from the second element of the array and does not include the fourth array element.
  • Output the result of NewArray.slice(-2,-1) for the fourth time, representing the extraction from the penultimate array element to the penultimate array element, and does not include the penultimate array element, so the extraction result is only There is "D".
  • The result of the fifth output is the original array.
As can be seen from the above example results, the array will not be changed after the JavaScript Array slice is processed, but JavaScript Array slice returns a new array, so the results of the first output and the fifth output are the same, because the original array It hasn't been modified. What needs to be overcome when using JavaScript Array slice is the position of the array elements. The first array element is calculated from 0. With this concept, practice makes perfect if you try several times.

The slice method is a feature that exists after JavaScript version 1.2. Almost all mainstream browsers support the JavaScript Array slice method.

Post a Comment

0 Comments