Methods of arrays (top)

Source: Internet
Author: User

1.join ()

The Array.join () method converts all elements in an array to strings and joins together, returning the last generated string. You can specify an optional string in the resulting string to split the elements of the array. If you do not specify a delimiter, commas are used by default. As shown in the following code:

var a = [three-to-one]//create an array with three elements

A.join ();//"The"

A.join (""); "1 2 3"

A.join (""); "123"

var B = new Array (10); An empty array of length 10

B.join ('-'); '---------': a string of 9 hyphens

  

The Array.join () method is the inverse of the String.Split () method, which splits the string into blocks to create an array

2.reverse ()

The Array.reverse () method reverses the order of the elements in the array, returning an array of reverse. It takes a substitution; in other words, it does not create new arrays by rearranging the elements, but instead rearranges them in the original array. For example, the following code uses the reverse () and join () methods to generate the string ' 3,2,1 ':

var a = [n/a];

A.reverse (). Join ()//' 3,2,1 ' and now A is [3,2,1]

3.sort ()

The Array.Sort () method sorts the elements in the array and returns the sorted array. When sort () is called without parameters, the array elements are sorted alphabetically (if necessary, the temporary conversion to a string is compared):

var a = new Array ("Banana", "cherry", "Apple");

A.sort ();

var s = a.join (","); s = = "Apple,banana,cherry"

If the array contains undefined elements, they are queued to the end of the array.

In order to sort the array in other ways rather than alphabetically, you must pass a comparison function to the sort () method. The function determines the order in which its two parameters are ordered in the sorted array. Assuming that the first argument should be before, the comparison function should return a value less than 0. Conversely, assuming that the first argument should be followed, the function should return a value greater than 0. Also, assuming that two values are equal (that is, their order does not matter), the function should return 0. So, for example, with a numeric size instead of an alphabetical order, the code looks like this:

var a = [33,4,1111,222];

A.sort (); Alphabet Order: 1111,222,33,4

A.sort (function (A, B) {//numeric order: 4,33,222,1111

return a-B; Returns a negative number, 0, a positive number, according to the order

})

A.sort (function (A, b) {return b-a});//The reverse order of the numeric size

Note that it is convenient to use an anonymous function expression here. Since the comparison function is used only once, there is no need to name them.

Another example of array element arrangement might be to perform a case-insensitive alphabetical sort of an array of strings, and the comparison function first converts the arguments to lowercase strings (using the toLowerCase () method) before starting the comparison:

A = [' Ant ', ' Bug ', ' cat ', ' Dog ']

A.sort (); Case-sensitive sorting: [' Bug ', ' Dog ', ' ant ', ' cat ']

A.sort (function (s,t) {

var a = S.tolowercase ();

var B = t,tolowercase ();

if (a<b) return-1;

if (a>b) return 1;

return 0;

}); [' Ant ', ' Bug ', ' cat ', ' Dog ']

4.concat ()

The Array.concat () method creates and returns a new array whose elements include the elements of the original array called concat () and each parameter of concat (). If any of these parameters itself is an array, then the elements of the array are connected, not the array itself. But be aware that each parameter of concat (). If any of these parameters itself is an array, then the elements of the array are connected, not the array itself. Note, however, that concat () does not recursively flatten the array. Concat () also does not modify the called array.

var a = [[+]

A.concat (4,5)//return [1,2,3,4,5]

A.concat ([4,5]); Back to [1,2,3,4,5]

A.concat ([4,5],[6,7]); Back to [1,2,3,4,5,6,7]

A.concat (4,[5,[6,7]); return [1,2,3,4,5,[6,7]]

5.slice ()

The Array.slice () method returns a fragment or sub-array of the specified array. Its two parameters specify the position of the beginning and end of the fragment, respectively. Returns an array of elements that contain the position specified by the first parameter and all the arrays to but not including the position specified by the second parameter. If you specify only one parameter, the returned array will contain all elements from the start position to the end of the array. If a negative number appears in the argument, it represents the position of the last element in relation to the array only. For example, parameter 1 specifies the last element, and 3 specifies the third-to-bottom element. Note that slice () does not modify the called array. Here are some examples:

var a = [1,2,3,4,5];

A.slice (0,3); back to [+]

A.slice (3); Back to [4,5]

A.slice (1,-1); Back to [2,3,4]

A.slice ( -3,-2); Back [3]

6.splice ()

The Array.splice () method is a common method for inserting or deleting elements in an array. Unlike slice () and concat (), splice () modifies the called Array. Note that splice () and slice () have very similar names, but their functions are fundamentally different.

Splice () can delete an element from an array, insert an element into an array, or do both of these things. Array elements after inserting or deleting points or increasing or decreasing their index values as needed, so the rest of the array remains contiguous. The first parameter of splice () specifies the starting position of the insertion and/or deletion. The second parameter specifies the number of elements that should be removed from the array. If the second argument is omitted, all elements are deleted from the starting point to the end of the array. Splice () returns an array of deleted elements, or an empty array if no elements are deleted. For example:

var a = [1,2,3,4,5,6,7,8,9];
var B = A.splice (4);
Console.log (A, b); a=[1,2,3,4] return to b=[5,6,7,8,9]

var a = [1,2,3,4,5,6,7,8,9];
var C = a.splice (UP);
Console.log (A,C); a=[1,4,5,6,7,8,9] return to c=[2,3]

var a = [1,2,3,4,5,6,7,8,9];
var d = A.splice (a);
Console.log (A,D); A = [1,3,4,5,6,7,8,9] return d=[2]

The first two parameters of splice () specify the array elements that need to be deleted. Any number of arguments immediately following it specifies the element that needs to be inserted into the array, starting at the position specified by the first parameter. For example:

var a = [1,2,3,4,5];
var B = a.splice (1,0, ' A ', ' B ');
Console.log (A, b); A = [1, ' A ', ' B ', 2,3,4,5] returns B = []

var a = [1,2,3,4,5];
var B = A.splice (2,2,[1,2],3);
Console.log (A, b); A = [1,2,[1,2],3,5] returns B = [3,4]

Note that, unlike concat (), splice () inserts the array itself rather than the elements of the array.

7.push () and Pop ()

The push () and Pop () methods allow arrays to be used as stacks. The push () method adds one or more elements to the end of the array and returns the new length of the array. The Pop () method is the opposite: it deletes the last element of the array, reduces the length of the array, and returns the value it deletes. Attention. All two methods modify and replace the original array instead of generating a new array of the modified version. Combining push () and pop () can be used to implement an advanced post-out stack with javascript arrays. For example:

  

var stack = []; Stack:[]
(Stack.push); stack:[1,2]

  Stack.pop (); STACK:[1] returns 2
Stack.push (3); stack:[1,3] returns 2
Stack.pop (); STACK:[1] returns 3

8.unshift () and Shift ()

The behavior of the Unshift () and Shift () methods is very similar to push () and pop (), unlike the insertion and deletion of elements in the head of an array rather than the tail. Ushift () adds one or more elements to the head of the array and moves the existing elements to a higher index to get enough space and finally returns the new length of the array. Shift () deletes the first element of the array and returns it, and then returns the new length of the array. Shift () deletes the first element of the array and moves the existing element to a higher index to get enough space and finally returns the new length of the array. Shift () deletes the first element of the array and returns it, and then moves all subsequent elements down one position to fill the empty array header. For example:

var a= []; A:[]

A.unshift (1); A:[1] returns 1

A.unshift (22); a:[22,1] returns 2

A.shift (); A:[1] returns 22

A.unshift (3,[4,5]); a:[3,[4,5],1] returns 3

A.shift (); a:[[4,5],1] returns 3

A.shift (); A:[1] return [4,5]

A.shift (); A:[] returns 1

Note: When you call Unshift () with multiple parameters, it behaves surprisingly. The parameter is inserted once (like the splice () method) instead of one at a time. This means that the order of the elements inserted in the final array is consistent with the order in which they are in the argument list. And if the elements are inserted one at a time, their order should be reversed.

9.toString () and toLocaleString ()

Arrays have the ToString method, just like other JavaScript objects. For an array, the method converts each of its elements to a string (the ToString () method of the calling element, if necessary) and outputs a comma-delimited list of strings. Note that the output does not include delimiters for square brackets or any other package values. For example:

[1,2,3].tostring ()//Generate ' a '

["A", "B", "C"].tostring ()//Generate ' a,b,c '

[1,[2, ' C ']].tostring ()//Generate ' 1,2,c '

Note that the string returned by calling the join () method that does not apply to any parameter is the same.

Tolocalstring () is a localized version of the ToString () method. It invokes the element's Tolocalstring () method to convert each array element to a string, and uses a localized (and custom implemented) delimiter to concatenate the strings to produce the final string.

Methods of arrays (top)

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.