A detailed description of array method in JavaScript

Source: Internet
Author: User

ECMAScript 3 defines some useful functions for manipulating arrays in Array.prototype, which means that these functions are available as methods of any array.

1. Array.join () method

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 symbol or string to separate the elements of the array in the resulting string. If you do not specify a delimiter, commas are used by default. Note: This method does not change the original array

var arr = [' A ', ' B ', ' C '];console.log (Arr.join ()); A,b,cconsole.log (Arr.join ("")); A b cconsole.log (Arr.join ("")); Abcconsole.log (Arr.join ("SLF")); Aslfbslfcvar arr2 = new Array (ten); Console.log (Arr2.join ("-")); // ---------

Extension: the Array.join () method is the reverse operation of the String.Split () method, which splits the string into blocks to create an array.

2. Array.reverse () method

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. Note: This method changes the original array.

var arr = [' A ', ' B ', ' C '];console.log (Arr.reverse ()); [' C ', ' B ', ' A ']console.log (arr); [' C ', ' B ', ' a ']

3. Array.Sort () method

The Array.Sort () method sorts the elements in the array and returns the sorted array. When the sort () method is called without parameters, the array elements are sorted in alphabetical order. Note: This method changes the original array.

var arr = [' ba ', ' B ', ' AC '];console.log (Arr.sort ()); [' AC ', ' B ', ' BA ']console.log (arr); [' AC ', ' B ', ' BA ']

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

var arr = new Array (4); arr[0] = ' ba '; arr[1] = ' B '; arr[2] = ' ZC '; arr[3] = Undefined;console.log (Arr.sort ()); [' AC ', ' B ', ' BA ', Undefined]console.log (arr); [' AC ', ' B ', ' BA ', undefined]

If you want to sort the array in other ways rather than in the alphabet order, 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 is in front, the comparison function should return a value less than 0. Conversely, assuming that the first argument is behind, the function should return a value greater than 0. Also, assuming that two values are equal (their order does not matter), the function should return 0. For example, to sort an array with a numeric size instead of an alphabetical order, the code is as follows:

var arr = new Array (4); arr[0] = 45;arr[1] = 12;arr[2] = 103;arr[3] = 24;console.log (Arr.sort ()); [103, 45]console.log, Arr.sort (function (A, b) {return b-a;})); [103, 45, 24, 12]

Sometimes you need to perform a case-insensitive alphabetical sort of a string array, and you can use the comparison function to first convert the arguments to lowercase strings (using the toLowerCase () method) before starting the comparison.

var arr = [' abc ', ' Def ', ' BoC ', ' FED '];console.log (Arr.sort ()); ["BoC", "Def", "FED", "abc"]console.log (Arr.sort (function (s, t) {var a = S.tolowercase (); var B = t.tolowercase (); if (A & Lt b) Return-1;if (a > B) return 1;return 0;}); ["ABC", "BoC", "Def", "FED")

4. Array.concat () method

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. Note, however, that concat () does not recursively flatten an array of arrays. Note: This method
The original array is not modified.

var arr = [' abc ', ' Def ', ' BoC ', ' FED '];console.log (Arr.concat (1, 2)); ["ABC", "Def", "BoC", "FED", 1, 2]console.log (Arr.concat (1, 2, [4, 5])); ["ABC", "Def", "BoC", "FED", 1, 2, 4, 5]console.log (Arr.concat (1, 2, [4, [' SLF ', 5]])); ["ABC", "Def", "BoC", "FED", 1, 2, 4, Array[2]]console.log (arr); ["ABC", "Def", "BoC", "FED"]

5. Array.slice () method

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. The returned array contains all the array elements between the position specified by the first parameter and all the positions specified by the second parameter (but without the position specified by the second argument). 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 relative to the last element in the array. For example, parameter 1 specifies the last element, and 3 specifies the third-to-bottom element. Note that this method does not modify the original array.

var arr = [' abc ', ' Def ', ' BoC ', ' FED ', ' SLF '];console.log (Arr.slice (1, 2)); ["Def"]console.log (Arr.slice (3)); ["FED", ' SLF ']console.log (arr.slice (0,-1)); [' abc ', ' Def ', ' BoC ', ' FED ']console.log (-arr.slice (-3,-1)); [' BoC ', ' FED ']console.log (arr); [' abc ', ' Def ', ' BoC ', ' FED ', ' SLF ']

6. Array.splice () method

The Array.splice () method is a common method for inserting or deleting elements in an 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 the insertion or deletion point increase or decrease 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 you omit the second argument, all elements from the starting point to the end of the array are deleted. Splice () returns an array of deleted elements, or an empty array if no elements are deleted. Note: This method changes the original array. (unlike concat (), splice () inserts the array itself rather than the elements of the array. )

var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9];console.log (Arr.splice (7)); [8, 9]console.log (arr); [1, 2, 3, 4, 5, 6, 7]console.log (Arr.splice (2, 4)); [3, 4, 5, 6]console.log (arr); [1, 2, 7]console.log (Arr.splice (2, 1, 3, 4, 5, 6)); [7]console.log (arr); [1, 2, 3, 4, 5, 6]console.log (Arr.splice (3, 2, 3, [1, 2, 3])); [4, 5]console.log (arr); [1, 2, 3, [1, 2, 3], 6]

7, Array.push () and Array.pop () methods

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. Note: Both methods will modify the original array.

var arr = [1, 2, 3];console.log (Arr.push (7)); 4console.log (arr); [1, 2, 3, 7]console.log (Arr.push ([2, 4])); 5console.log (arr); [1, 2, 3, 7, [2, 4]]console.log (Arr.pop ()); [2, 4]console.log (arr); [1, 2, 3, 7]

8, Array.unshift () and Array.shift () methods

The behavior of the Unshift () and Shift () methods is very similar to the push () and Pop () methods, not the same: the former is the insertion and deletion of elements in the head of the array. Unshift () 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 the deleted element, then moves all subsequent elements forward one position to fill the empty array header. Note: Both methods will modify the original array.

var arr = [6, 2, 3, 4, 5, 6];console.log (Arr.shift ()); 6console.log (arr); [2, 3, 4, 5, 6]console.log (Arr.unshift ([' A ', ' B '])); 6console.log (arr); [[' A ', ' B '], 2, 3, 4, 5, 6]console.log (Arr.unshift (' A ', ' B ')); 8console.log (arr); [' A ', ' B ', [' A ', ' B '] 2, 3, 4, 5, 6]

Note that when Unshift () is called with multiple parameters, if the parameter is inserted one time instead of at a single insertion, this means that the order of the elements inserted in the final array is consistent with the order in which they are in the parameter list. And if the elements are inserted one at a time, their order should be reversed.

var arr = [6, 2, 3, 4, 5, 6];console.log (Arr.unshift (' A ', ' B ', ' C ')); 9console.log (arr); [' A ', ' B ', ' C ', 6, 2, 3, 4, 5, 6]console.log (Arr.unshift (1)); 10console.log (arr); [1, ' A ', ' B ', ' C ', 6, 2, 3, 4, 5, 6]console.log (Arr.unshift (2)); 11console.log (arr); [2, 1, ' A ', ' B ', ' C ', 6, 2, 3, 4, 5, 6]

9, Array.tostring () and array.tolocalestring () methods

Arrays have the ToString () method as with other JavaScript objects. For arrays, this method converts each element of an array to a string and outputs a comma-delimited list of strings. Note: This method does not modify the original array (this is the same as the string returned by calling the join () method without using any parameters)

var arr = [1, 2, 3];console.log (arr.tostring ()); 1,2,3console.log (typeof (Arr.tostring ()))//Stringconsole.log (arr); [1, 2, 3]

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

Summary: The array array method described above, a total of 12 methods, these methods are defined in ECMAScript 3. The Array.join () method, the Array.concat () method, the Array.slice () method, the Array.tostring method, and the Array.tolocalestring () method do not change the original array. When the other 7 array methods are executed, the original number is changed.

A detailed description of array method in JavaScript

Related Article

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.