Array Knowledge Encyclopedia

Source: Internet
Author: User
Tags array length

Conversion method

As mentioned earlier, all objects have tolocalestring (), toString (), and valueof () methods. Where the ToString () method of the call array returns a comma-delimited string that is stitched together as a string of each value in the array. Instead, the call to ValueOf () returns an array . In fact, in order to create this string, the ToString () method of each item of the array is called.

In addition, the tolocalestring () method often returns the same values as the ToString () and valueof () methods, but not always. when the toLocaleString () method of an array is called, it also creates a comma-delimited string of array values. The only difference from the first two methods is that this time, in order to get the value of each item, the toLocaleString () method of each item is called, not the ToString () method. Array inherits the toLocaleString (), toString (), and valueof () methods, array items are returned by default as a comma-delimited string. If you use the Join () method, you can use a different delimiter to construct the string. The join () method receives only one parameter, which is a string used as a delimiter, and then returns a string containing all the array items. If the value of an item in the array is null or undefined, the value is represented as an empty string in the results returned by the join (), tolocalestring (), toString (), and valueof () methods. Stack method Stack method The ECMAScript array also provides a way for the array to behave like other data structures. Specifically, an array can behave like a stack, which is a data structure that restricts the insertion and deletion of items. The stack is a LIFO (Last-in-first-out, LIFO) data structure, where the most recently added item is first removed. The insertion of the item in the stack (called Push-in) and the removal (called popup) occur only in one place-the top of the stack. ECMAScript is an array-specific the Push () and Pop () methods are availableTo implement a stack-like behavior.Queue Method

The access rules for the stack data structure are LIFO (LIFO), whereas the access rules for the queue data structure are FIFO (first-in-first-out, first-in-one-out). The queue adds items at the end of the list, removing items from the front end of the list. Because push () is a method of adding items to the end of an array, it takes only one method to get an item from the front of the array to simulate the queue. The array method for doing this is shift (), which moves the first item in the array and returns the item, minus 1 of the length. Using the shift () and push () methods together , you can use arrays as you would with queues. ECMAScript also provides an array of unshift () methods for the arrays. As the name implies,Unshift () is the opposite of the use of Shift () : It can add any item to the front of the array and return the length of the new array. Therefore, using the Unshift () and Pop () methods simultaneously, you can simulate the queue in the opposite direction by adding items to the front end of the array, removing items from the end of the array,

Reorder Methods
There are already two methods in the array that can be used for reordering directly: reverse () and sort (). The action method ECMAScript provides a number of ways to manipulate the items already contained in the array. which the Concat () method can create a new array based on all the items in the current array。 Specifically, This method will first create a copy of the current arrayThe received parameters are then added to the end of the copy, and the newly constructed array is returned at the end. Without passing arguments to the concat () method, it simply copies the current array and returns a copy. If one or more arrays are passed to the concat () method, the method adds each item in the array to the result array. If the value passed is not an array, the values are simply added to the end of the result array.

The next method is slice (), which can create a new array based on one or more items in the current array. the Slice () method can accept one or two parameters, that is, to return the starting and ending positions of an item. In the case of only one argument, the slice () method returns all items starting at the specified position of the parameter to the end of the current array. If there are two parameters, the method returns the item between the start and end positions--but not the end position. Note that theslice () method does not affect the original array.

If the slice () method has a negative number in its argument, the array length is used to determine the corresponding position. For example, calling Slice ( -2,-1) on an array containing 5 items is the same as calling slice (3,4). If the end position is less than the start position, an empty array is returned.  Let's introduce the splice () method, which is probably the most powerful array method, and it is used in many ways. The main purpose of 1splice () is to insert items into the middle of the array, but there are 3 ways to use this method. Delete: You can delete any number of items by specifying only 2 parameters: the location of the first item to delete and the number of items to delete. For example, splice (0,2) deletes the first two items in the array.

Insert: You can insert any number of items to a specified location, providing only 3 parameters: Start position, 0 (number of items to delete), and items to insert. If you want to insert more than one item, you can pass in four, five, or any number of items.    For example, splice (2,0, "Red", "green") will insert the string "red" and "green" starting at position 2 of the current array.

Replace: You can insert any number of items at the specified location and delete any number of items at the same time, specifying only 3 parameters: The starting position, the number of items to delete, and any number of items to insert. The number of items inserted does not have to be equal to the number of items deleted. For example, splice (2,1, "Red", "green") removes the entry for the current array position 2, and then inserts the string "Red" and "green" starting at position 2.

The splice () method always returns an array that contains the items that were deleted from the original array (an empty array is returned if no items were deleted).






Array Knowledge Encyclopedia

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.