The slice javascript () method

Array object in JavaScript provides a slice () method that returns the selected elements from the existing array.

arrayObject.slice(start, end)

Parameter Description

start Required (or no meaning). Is selected from a predetermined start where, i.e. at the start of extraction index (starting from 0), the start array element extracted from the original index. If it is negative, then it provides an array of tail begin to run from the location. That is, the last element refers -1, -2 means penultimate element, and so on. If this parameter is not specified, the index from zero. If the parameter is greater than the original length of the array, it will return an empty array.
end Optional. Where the end of a predetermined selection, the index is an array of fragments at the end of the array, i.e., to the end of the extraction index (starting from 0), the array element at the end of the extraction of the raw index, which extracts from the original array index All the elements start to end (including start, but does not include end). If this parameter is negative, then it is prescribed begin to run from the end of the array elements. If this parameter is not specified, then sliced ​​array contains all of the elements from the start to the end of the array. If the parameter is greater than the length of the array will always be retrieved at the end of the original array.

return value

Returns a new array containing elements from start to end (not including the element) in the arrayObject.

Precautions

This method does not modify the original array, but returns a shallow copy of a new array element of the original array. Shallow copy means is that, if the two arrays Array.push in any one of () adds a new element or Array.splice () method removes the element, the other is not affected; but if modified elements in the array, and the situation is because of the different types of elements in the array varies.

1. If the element is an object reference (not the actual object), slice () method will copy the object reference to the new array. In other words, two arrays of these two elements refer to the same object, if the referenced object is changed, the new and original array elements will synchronize this change.

2. For strings, numbers, Boolean values, and is (not String, Number or Boolean object), Slice () method will be copied to the new array values. Modify these strings or numbers or a Boolean value in another array, the array will not affect the other.

Simple Example 1

var arr = ["yanggb1", "yanggb2", "yanggb3"];
var arr1 = arr.slice(1, 2); // ["yanggb2"]

Simple Example 2

var arr = ["yanggb1", "yanggb2", "yanggb3"];
var arr1 = arr.slice(1); // ["yanggb2", "yanggb3"]

Clever use

This method is very easy to use cut-off date in the scene formatting needs, we have to feel it.

var Today = '2019-05-20' ;
 // I want to form yyyy-MM-dd of 
var today1 today.split = ( '-') Slice (0) .join ( '-');. // 2019 -05-20 
// I want the form MM-dd 
var today1 today.split = ( '-') Slice (. 1) .join ( '-');. // 05-20 
// I want to dd form 
var . today2 today.split = ( '-') Slice (2) .join ( '-'); // 20 is

If you want to switch to a different form of date, only you need to modify the slice () method parameters can be very flexible.

 

"In fact, life in the well is also very good, that is, occasionally, feel very empty world, life is very salty."

Guess you like

Origin www.cnblogs.com/yanggb/p/11800788.html