Summary and analysis of JavaScript Array methods, Summary of javascript Array

Source: Internet
Author: User
Tags javascript array

Summary and analysis of JavaScript Array methods, Summary of javascript Array

Since freecodecamp has recently been used for code refreshing and many JavaScript Array methods, I have made a summary of the JavaScript tutorial. The specific content is as follows:

I. Common Methods

1. join () concatenates array elements and returns them as strings.
Parameter: (optional) delimiter between elements. If no parameter is specified, it is a comma by default.
Return Value: String
Impact on the original array: None

2. reverse () converts the element order of the array into reverse order.
Parameter: None
Returned value: Array
Impact on the original array: the original array is changed to the array after the inverted order

3. sort () sorts array elements and returns
Parameter: (optional) sorting method function. If no parameter is specified, the function is sorted alphabetically.
Returned value: sorted Array
Impact on the original array: the original array is changed to the sorted Array

4. concat () connects several Arrays
Parameter: Several, which can be an array or an element,
Returned value: New array after connection
Impact on the original array: None

5. slice () captures several elements from the array to form a new array and returns
Parameter: two numbers. The second parameter is optional. The first parameter indicates the index value of the first element to be truncated (this element is included during truncation). If the first parameter is negative, the truncated element starts from the end of the element (for example,-1 indicates the last element ); the second parameter indicates the index value of the element to be intercepted (this character is not included in the screenshot). The negative value is the same as that of the first parameter.
Return Value: New truncated Array
Impact on the original array: None

6. splice () replaces, deletes, or inserts elements from the array and returns the new array.
Parameters: several parameters. The first parameter is required. The other parameter is optional. The first parameter is the first index value of the operation. If there is no second parameter, all elements after the index value of the first parameter (including the first parameter) will be deleted. If the second parameter is included, the second parameter will delete the number of elements, returns a new array composed of these elements. When the second parameter is 0, the following parameters Insert the original array as the new element and return an empty array; when the second parameter is not 0 and contains other parameters, the replacement operation is performed and a new array consisting of the original elements is returned.
Return Value: delete a new array composed of elements.
Impact on the original array: replaces, deletes, and inserts the original array.

7. push () adds elements to the end of the array and returns the length of the array.
Parameters: several elements added to the end of the array
Returned value: the length of the array after the element is added.
Impact on the original array: elements added to the end of the original array

8. pop () deletes an element from the end of the array.
Parameter: None
Returned value: the deleted element.
Impact on the original array: an element is deleted at the end of the original array.

9. unshift () adds elements to the array header and returns the array length.
Parameters: several elements added to the array Header
Returned value: the length of the array after the element is added.
Impact on the original array: elements added to the original number Header

10. shift () deletes an element from the array header.
Parameter: None
Returned value: the deleted element.
Impact on the original array: an element is deleted from the original array header.

11. toString () converts an array to a string. Each element is separated by commas.
Parameter: None
Returned value: the string to be formed (the two-dimensional array only connects the elements of the two-dimensional array)
Impact on the original array: None

12. toLocaleString () is the localized version of The toString () method.


--------------------------------------------------------------------------------

Ii. iterator Method

1. foreach () calls the method for each element of the array
Parameter: A Function
Return Value: None
Impact on the original array: None

2. every () accepts a function whose return value is boolean. If all elements in the array return true, true is returned. Otherwise, false is returned.
Parameter: a function with a Boolean return value.
Return Value: true or false
Impact on the original array: None

3. some () accepts a function whose return value is boolean. If an element exists in the array and the function returns true, true is returned. Otherwise, false is returned.
Parameter: a function with a Boolean return value.
Return Value: true or false
Impact on the original array: None

4. map () accepts a function as a parameter and returns a new array. The elements of the new array are the results of the original array element using this function.
Parameter: A Function
Returned value: An array consisting of the result values of each function.
Impact on the original array: None

5. filter () receives a function that returns a Boolean value as a parameter. It should be a function for all elements and return a new array composed of elements whose return value is true.
Parameter: A Function
Returned value: An array composed of elements whose function is true.
Impact on the original array: None


--------------------------------------------------------------------------------

Iii. Merge Methods

1. reduce () accepts a function as a parameter and returns a value. Starting from an accumulated value, this function is continuously called for the accumulated value and subsequent elements in the array.
Parameter: A Function
Returned value: the final cumulative value.
Impact on the original array: None

2. reduceRight () method
Note: Like reduce, the execution sequence is from right to left.

The summary and analysis of the above JavaScript array method is all the content shared by Alibaba Cloud. I hope to give you a reference and support for the customer's house.

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.