Summary of array methods in JavaScript

Source: Internet
Author: User
Tags foreach arrays comparison join joins sort

Array.prototype defines a number of ways to manipulate arrays, here are some of the methods in ECMASCRIPT3

1.array.join () method

This method converts the elements in an array to strings and joins them together with the specified symbols, returning the last generated string, which can contain a parameter, a symbol that joins the array element, and a comma by default.

?
1 2 3 4 5 6 7 8 var ay = [1,2,3]; Ay.join (); => "1,2,3" Ay.join ("+"); => "1+2+3" Ay.join (""); => "1 2 3" Ay.join (""); => "123" var by = new Array (10)//New an empty array of length 10 by.join ("-"); => "---------" connects 10 empty elements

2.array.reverse () method

This method reverses the order of elements in an array, returning an array in reverse, which changes the current array without creating a new array.

The

code is as follows:


var a = [1,2,3];


A.reverse (). join (); => "3,2,1", at this time a=[3,2,1]

3.array.sort () method

This method sorts the elements in the array and returns the sorted array. When the sort () method takes no arguments, the array is sorted alphabetically, and if the array contains undefined elements, it is queued to the tail of the array.

The

code is as follows:


var as = ["Banana", "cherry", "Apple"];


As.sort ();


As.join ("+"); => "Apple+ banana+ Cherry"

We can also pass a comparison function as an argument to the sort () method to sort the array by the specified comparison function. The comparison function returns a value less than 0, the first argument is before, the opposite return value is greater than 0, then the second argument is equal to the previous two parameter values, then returns 0

The

code is as follows:


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


Sy.sort (); => "1111,222,33,4"


Sy.sort (function (a,b) {


return a-b;


}); => "4,33,222,1111"


Note: It is most appropriate to use anonymous functions here, because they are called only once, without specifying the function name

4.array.concat () method

The method creates and returns a new array, joins the original array elements and each element of the method, and makes up a new array. The method does not recursively call the arguments in the method.

The

code is as follows:


var a = [1,2,3];


A.concat (4,5); => "1,2,3,4,5"


A.concat ([4,5]); => "1,2,3,4,5"


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


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

5.array.slice () method

This method returns a fragment or a child array of the specified array, which can have two parameters, each setting the start and end of the fragment, and the returned array contains the element specified by the first parameter and all the array elements that are not specified by the second argument. If there is only one argument, the argument contains a negative value from the specified start position to the end of the array, representing the position relative to the last element in the array. The method does not modify the array being called.

The

code is as follows:


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


D.slice (1,2); => "2"


D.slice (1,-1); => "2,3,4"


D.slice (3); => "4,5"


D.slice ( -3,-1); => "3,4"

6.array.splice () method

This method is a common method of inserting or deleting elements in an array, which modifies the original array. The method can contain multiple parameters, and the first parameter specifies the starting position to insert or delete in the array. The second parameter specifies the number of elements to be deleted, and if unspecified, the start position and all subsequent elements are deleted, and the parameters after the two parameter specify the element that is inserted into the array, which returns an array of deleted elements.

1 2 3 4 5 6 7 var e = [1,2,3,4,5,6]; E.splice (4); => return [5,6]; E is [1,2,3,4] e.splice (1,2); => return [2,3]; E is [1,4] var f = [1,2,3,4,5]; F.splice (2,0, "a", "B"); => return []; f is [1,2,a,b,3,4,5] F.splice (2,2,[6,7],3); => return [a,b]; f is [1,2,[6,7],3,4,5]

7.push () and Pop () methods

These two methods use an array as a stack, and the push () method adds one or more elements to the end of the array and returns the length of the array. The Pop () method deletes the last element of the array, reduces the length of the array, and returns the deleted value.

8.unshift () method and Shift () method

The two methods are to add a delete operation in the array header, and the Unshift () method is to add one or more elements to the array header to return the length of the array. The shift () method deletes the first element of the array and returns.

 
1 2 3 4 5 6 var a=[]; [] A.push (1,2); [1,2] A.pop (); [1] a.unshift (2,3); [2,3,1] A.shift (); [3,1]

9.toString () and toLocaleString () method

The two methods are to convert each element of the array into a string, and toString () converts each element into a string and the output is separated by commas. The toLocaleString () method is to convert each element of the array called tolocalestring () into a string and connect using a localized separator.

The following is an introduction to the array methods peculiar to several ECMAScript5, first of all, to make a general understanding before introducing the method. The first parameter of most methods takes a function, and the function is called once for each element of the array, and if the sparse array is said, the element that does not exist does not call the function. In most cases, the called function uses three parameters: the array element, the index of the element, and the array itself.

1.forEach () method

The method traverses the array from beginning to end, and each element of the array invokes the specified function. The method does not terminate until all the array elements are traversed. If you want to terminate prematurely, you must place the foreach () in a try block and throw an exception.

 
1 2 3 4 5 6 7 8 9 var data=[1,2,3,4,5] var sum = 0; Data.foreach (function (value) {//=>value array element sum+=value;})//=>15 Data.foreach (function (value,i,a) {//=> Three parameters refer to algebraic group elements respectively, element index and array a[i] = v+1; })//=>data=[2,3,4,5,6]

2.map () method

This method passes each element of the array to the specified function and returns a new array that contains the return value of the array element invocation function. If it is a sparse array, the new array returned is also an array of coefficients for the same structure.

1 2 3 4 var a=[1,2,3]; var b=a.map (function (v) {return v*v;})//=> b=[1,4,9]

3.filter () method-similar to conditional filtering

The method returns a subset of the original array, the passed function is used to make a logical decision, returns TRUE or false, and if the returned value is true or can be converted to true, the current array element is a member of the subset, added to the returned array. This method skips the empty elements of a sparse array.

 
1 2 3 4 5 6 7 var a=[5,4,3,2,1] Var smalla=a.filter (function (v) {return v<3;})//=> returns the [2,1] var everya=a.filter (function (v,i) {/ /=>i represents the element index return i%2==0; })//=> return [5,3,1]


4.every () and some () method

The two methods are logically determined by the array, and each element of the array uses the specified function to return TRUE or false.
The Every () method returns True if and only if all elements in the array call the decision function to return true, or false.
The Some () method returns True if at least one element in the array calls the decision function to return true, otherwise it returns false.

Both methods are not traversing the array element once the return value is confirmed.

5.reduce () and Reduceright () method

The two methods combine the array elements using the specified function to generate a single value.
Reduce () requires two parameters, the first is the action function that performs the simplification combination, and the second is the initial value of the combination. Unlike the previous methods, the common three parameters (array elements, element indices, and arrays themselves) are passed to the function as 2~4 arguments to the Operation function, the first of which is the result of the calculated combination so far.
If you are targeting an empty array, calling the reduce () method when you do not specify an initial value can result in a type error exception.
The Reduceright () method works the same as the reduce () method, with the difference that it is processed from high to low by an array index (that is, merge from right to left)

6.indexOf () and LastIndexOf () method

Both of these methods are used to search the entire array for a given value and return the index value of the first matching element, and if not, the -1.indexof () method is searched from start to finish, and lastindexof () is searched from the end of the head.

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.