JS provides a number of ways to operate an array, and what is intended to share, delete, change, and check in arrays.
can receive any number of parameters, add them one by one to The array ends and returns the length of the modified number of groups. For example:
2, UNSHIFT ()
This method is similar to PUSH (), and can receive any number of parameters, but it is simply added to an array front end. Also return to the new array length. Let’s follow the example:VAR len = arr.unshift (0); console.log (arr); // [0, 1, 2, 3] Console .log (len); // 4len = arr.unshift (-2, -1); console.log (arr); // [-2, -1, 0, 1, 2, 3] Console.log (LEN ); //6
3, concat ()
This method is similar to the Push () method, which is also The element is added to the end of the array, but this array is not the original array, but its copy, so the constru will return one after the array.A new array. The specific usage is as follows:
1 Do not pass the parameters, return to the current array copy
3 transmits non-array parameters, which will be directly added to the end of the result array
Continue to the top of the chestnut:
4, splice ()
The three methods of the previous method have a large limit, because it is an array before it is not added to an array, and splice () is Not the same, it is very flexible and powerful. Flexible is because it can add an elements to an array of any position, which is powerful because it has the function of deleting and replacing an element in addition to an element (this later will be said).
Still then the above example continues:Arr.Splice (3, 0, 0.2, 0.4, 0.6, 0.8); Console.log (Arr ); // [-2, -1, 0.8, 1, 2, 3]
As can be seen, splice () PUSH () and unshift () are modified directly on the original number group.
The use of the PUSH () method can be made first out Stack, the method can delete the last item from the end of the array and return to the item.
VAR item = arr.pop (); console.log (item); // 3console.log (arr); // [-2, -1, 0, 0.2, 0.4, 0.6, 0.8, 1, 2]
With the push () method, use can constitute a queue of advanced first, which deletes the first item of the array and returns the item. Continue to follow up:
VAR item = arr.shift (); console.log (item); // -2console.log (Arr ); // [-1, 0.8, 1, 2]
3, Slice ()
This method is the same as concat (), which does not affect the original array, but SLICE () is used to crop the number of groups, return to cropped arrays, the specific usage is as follows:
The slice () method can accept one or two parameters, that is, the start and end position of the item is returned. In the case of only one parameter, the slice () method returns all items that start from the parameter to the current array. If there are two parameters, the method returns an item between the start and end positions - but does not include an item ended.
VAR Arr2 = arr.slice (2, 6); console.log (arr); // [ 1, 0, 0.2, 0.4, 0.6, 0.8, 1, 2] Console.log (Arr2); // [0.2, 0.4, 0.6, 0.8]
4, splice ()
Top, the method needs to pass more than 3 parameters when adding array elements, and the second parameter is used to specify the number of points to delete elements, then we pass Number 0. Then, if you only need to delete the element, we just need to pass the two parameters, and the first parameter is used to specify the location of the first item to be deleted, and the second parameter is used to specify whether to delete an element. Number.
Continue to ~~
4 elements from the position of the index 2 start, so the result is [-1, 0, 1, 2].
This actually flexible way is to use splice () this powerful method, in fact, through the above understanding of the method, we are roughly You can know the basic principles of modifying array elements using this method.
The principle is simple, inserting any number of elements into the specified location, and deleting any number of elements at the same time.
Arr.splice (2, 1, 0.5, 1, 1.5); console.logArr); // [-1, 0, 0.5, 1, 1.5, 2]Fourth, Check
IndexOf () and LastIndexOf ()These two methods receive two parameters: Items to look up and (optional) represents the index of the lookup start position. Where indexof () starts from the beginning (position 0) of the array, the LastIndexOf () method starts looking forward from the end of the array.
When the element is not found, the -1, a LastIndexOf () method is used.The above is all the content of this article, I hope to help everyone, I hope everyone will support Tumi Clouds.