Learn JS authoritative guide Fifth station----array

Source: Internet
Author: User

1. Array methods

①join ()

Converts the left and right elements of the array into strings and joins together, returning the last generated string.

②reverse () change the original array

Reverses the order of the elements in the array, returning an array of reverse. Rearranged in the original array.

③sort () change the original array

Sorts the elements in the array and returns the sorted array, with the array elements sorted in alphabetical order when called without arguments. You can sort the sort by numeric values using the following code:

    var arr6_5 = [33,2,4,111];    Console.log (function (A, b) {return-A-B})//output: [2,4,33,111]

④concat () does not change the original array

Creates and returns a new array whose elements include the elements of the original array called concat () and each parameter of concat ().

⑤slice () does not change the original array

Returns a fragment or sub-array of the specified array. Two parameters specify the position of the beginning and end of the fragment, respectively. (contains the start position but does not contain the end position). If there is only one argument from that position to the end. A negative number represents the start at the end.

⑥splice () change the original array

A common method for inserting or deleting elements in an array. The first two parameters specify the array to be deleted, followed by any number of arguments that specify the elements that need to be inserted into the array.

Arr6_5_2 = [2, 4, A, 111, a.]   Arr6_5_2.splice (2,0, "a", "B", "C"); Remove 0 bits from the start of subscript 2 and insert "A", "B", "C"
Console.log (Arr6_5_2); =>[2, 4, "A", "B", "C", 33, 111, 99, 66]

⑦push () and pop () change the original array

The push () method adds one or more elements to the end of the array and returns the new length of the array. Pop () deletes the element at the tail of the array.

⑧unshift () and Shift () change the original array

The Unshift () method adds one or more elements to the head of the array and returns the new length of the array. Shift () removes the element from the head of the array.

⑨tostring () and tolocalstring ()

Array methods in 2.ES5

①foreach ()

Iterate through the array, which can be used with the ES6 arrow function. Without a break statement, if you want to terminate prematurely, you must put the foreach () method in a try block and throw an exception.

②map ()

Passes each element of the called array to the specified function and returns an array that contains the return value of the function.

Arr6_5_2 = [2, 4, 33, 111, 99, 66];  var arr6_5_3 = Arr6_5_2.map (function (x) {return 2*x});  Each value in the array is Console.log (Arr6_5_3); =>[4, 8, 66, 222, 198, 132]

③filter ()

Returns a subset of the called Array. The parameter is a function, which is equivalent to a filter condition.

④every () and some ()

Is the logical decision of arrays: They apply the specified function to the array element to determine, which returns TRUE or false.

⑤reduce () and Reduceright ()

Uses the specified function to combine array elements to produce a single value.

⑥indexof () and LastIndexOf ()

Searches for an element with the given value in the entire array, returns the index of the first element found, or returns 1 if it is not found.

3.isArray

In ES5, you can use the Array.isarray () function to determine whether it is an array. The Object.prototype.toString.call () method is used.

4. Class Array objects

is an object that has a length property and a non-negative integer that does not exceed 2^32. An array method can be called indirectly using the call method.

Array.prototype.join.call (arr, ' + ');

Learn JS authoritative guide Fifth station----array

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.