JavaScript array method summarizing and analyzing _javascript techniques

Source: Internet
Author: User
Tags array length joins javascript array

Thanks to the recent brush code on Freecodecamp and the use of many JavaScript array methods, a tutorial on JavaScript is organized, as follows:

I. Common methods

1. Join () joins the array elements together and returns as a string
Parameters: Optional, specifies the delimiter between elements, and the default is comma without arguments
return value: String
Effects on the original array: none

2, reverse () the sequence of elements of the array into reverse order return
Parameters: None
return value: Array
Effect on the original array: the original array was modified to an array after the descending order

3, sort () the array elements are sorted and returned
Parameters: Optional, sorted method functions, with no parameters, default sorted by dictionary order
Return value: Sorted array
Effect on the original array: the original array was modified to the sorted array

4. Concat () connect several arrays
Parameters: Several, can be arrays or elements,
Return value: The new array after the connection
Effects on the original array: none

5. Slice () truncates several elements from the array to form a new array and returns
Parameters: two digits, where the second optional, the first parameter represents the index value of the first element that begins the interception (contains this element when it is intercepted), and if the first argument is a negative number, the intercepted element begins at the end of the element (e.g.-1 for the last element) The value of the second parameter is the index of the element that stops the interception (this character is not included in the interception), and the negative is the same as the first parameter
Return value: The new array to intercept
Effects on the original array: none

6. Splice () replaces, deletes, or inserts elements from the array and returns the new array
Parameters: Several arguments, where the first argument is required, the other is optional, the first argument is the first index value of the operation, when there is no second argument, the first parameter (containing the first argument) is deleted after all elements, with the second argument, the second parameter deletes the number of elements. and returns a new array of these elements; When the second argument is 0 o'clock, the subsequent argument inserts the original array as the new element and returns an empty array, and when the second argument is not 0 and contains other arguments, the substitution operation is made and the new array of original elements before the substitution is returned.
Return value: Deletes a new array of elements
Effects on the original array: replace, delete, insert, and so on

7, push () Add elements to the end of the array and return the array length
Parameters: Several, elements added to the tail of the array
Return value: The length of the array after the element is added
Effect on the original array: elements added to the end of the original array

8. Pop () deletes an element from the tail of the array
Parameters: None
Return value: Deleted element
Effect on the original array: the tail of the original array is deleted an element

9, Unshift () Add elements to the head of the array and return the array length
Parameters: Several, elements added to the head of the array
Return value: The length of the array after the element is added
Effect on the original array: The original number is added to the header

10. Shift () remove an element from the array header
Parameters: None
Return value: Deleted element
Effect on the original array: the original array header was deleted an element

11, toString () converts the array to a string, separated by commas between each element
Parameters: None
Return value: The resulting string (also just the element that joins a two-dimensional array in a two-dimensional array)
Effects on the original array: none

12, toLocaleString () is a localized version of the ToString () method


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

Second, the Iterator method

1, foreach () each element of the array call method
Parameters: a function
return value: None
Effects on the original array: none

2. Every () accepts a function with a return value of Boolean type, all elements in the array, which returns True, returns True, otherwise returns false
Parameter: A function that returns a Boolean type
Return value: TRUE or False
Effects on the original array: none

3. Some () accepts a function that returns a Boolean type, returns True if the element in the array returns True, or returns false
Parameter: A function that returns a Boolean type
Return value: TRUE or False
Effects on the original array: none

4. Map () takes a function as a parameter, returns a new array whose elements are the result of the original array element using the function
Parameters: a function
Return value: An array of the result values for each element using the function
Effects on the original array: none

5. Filter () receives a function that returns a Boolean value as an argument, the function for all elements, and returns a new array of elements that return a value of true
Parameters: a function
Return value: Each element uses an array of elements that are true to the function
Effects on the original array: none


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

Third, the merging method

1. Reduce () takes a function as an argument and returns a value. Starting with an accumulation value, the function is called continuously on the cumulative value and subsequent elements in the array.
Parameters: a function
Return value: The last Cumulative value
Effects on the original array: none

2, Reduceright () method
Description: As with reduce, just the order of execution is from right to left

Above this JavaScript array method summary analysis is small series share to everybody's content, hoped can give everybody a reference, also hoped that everybody supports the cloud habitat community.

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.