The return value of the JS array method and whether to change the array summary

Source: Internet
Author: User

Concat () method

The Concat () method is used to concatenate two or more arrays.

The method does not alter the existing array, but only returns a copy of the concatenated array.

return value

Returns a new array. The array is generated by adding all the Arrayx parameters to the Arrayobject. If the argument for the concat () operation is an array, then the element in the array is added, not the array.

The join () method does not change the original array

The join () method is used to put all the elements in an array into a string.

The elements are delimited by the specified delimiter.

return value

Returns a string. The string is generated by converting each element of Arrayobject to a string and then connecting the strings to insert a separator string between the two elements.

Pop () change the original array

method is used to delete and return the last element of the array.

return value

The last element of the Arrayobject.

The Pop () method removes the last element of the arrayobject, reducing the length of the array by 1, and returning the value of the element it deletes. If the array is already empty, pop () does not change the array and returns the undefined value.

The push () method changes the original array

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

return value

Adds the specified value to the new length after the array.

Description

The push () method adds its parameter order to the tail of the arrayobject. It modifies the arrayobject directly, rather than creating a new array. The push () method and the Pop () method use the advanced post-stack functionality provided by the array.

Reverse () method changes the original array

The reverse () method reverses the order of the elements in the array.

The method changes the original array without creating a new array.

Shift () method to change the original array

The shift () method removes the first element from the array and returns the value of the first element.

return value

The value of the original first element of the array.

Description

If the array is empty, then the shift () method will not take any action, returning the undefined value. Note that the method does not create a new array, but instead modifies the original arrayobject directly.

The slice () method does not change the array if the argument is negative then it specifies the position from the end of the array. In other words, 1 refers to the last element, 2 refers to the second-lowest element, and so on.

The slice () method returns the selected element from an existing array.

return value

Returns a new array containing elements from start to end (excluding the element) from the Arrayobject.

Description

Note that the method does not modify the array, but instead returns a subarray. If you want to delete an element from an array, you should use Method Array.splice ().

The sort () method modifies the original array

return value

A reference to an array. Note that the array is sorted on the original array, and no replicas are generated.

Description

If the method is called without parameters, the elements in the array are sorted alphabetically, more precisely, by the order in which the characters are encoded. To do this, you should first convert the elements of the array to a string, if necessary, for comparison.

If you want to sort by other criteria, you need to provide a comparison function that compares two values and returns a number that describes the relative order of the two values. The comparison function should have two parameters A and B with the following return values:

    • If a is less than B, a value that is less than 0 is returned if a should appear before B in the sorted array.
    • If a equals B, 0 is returned.
    • If a is greater than B, a value greater than 0 is returned.

Splice () method to modify the original array

The splice () method adds/deletes an item to/from the array, and then returns the item that was deleted.

Note: This method changes the original array.

Arrayobject.splice (index,howmany,item1,....., ItemX)
Parameters Description
Index Necessary. An integer that specifies the location of the Add/remove item, using a negative number to specify the position from the end of the array.
Howmany Necessary. The number of items to delete. If set to 0, the item is not deleted.
Item1, ..., ItemX Optional. Adds a new item to the array.

Note that the splice () method is different from the slice () method, and the splice () method modifies the array directly

The ToString () method does not modify the original array

The ToString () method converts the array to a string and returns the result.

return value

The string representation of the Arrayobject. The return value is the same as the string returned by the join () method without parameters.

Unshift () method changes the original array

The Unshift () method adds one or more elements to the beginning of the array and returns the new length.

Note that the Unshift () method does not create a new creation, but instead modifies the original array directly.

The return value of the JS array method and whether to change the array summary

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.