Methods of the Array object in JavaScript

Source: Internet
Author: User

    • Detecting Arrays: You can use the value instanceof array to determine whether an object is an array if you consider only one global scope, and if you want to consider multiple global scopes, Use Object.prototype.toString.call (value) = = "[Object Array]"

    • Conversion Method:toString tolocalestring Join

    • Stack method:(LIFO) inserts an item at the end of the array and pops an item at the end of the array. Push: Inserts one or more entries at the end of the array and returns the current array length; pop: pops up the last item of the array and returns.

    • Queue method:(FIFO) inserts an item at the end of the array and pops an entry at the beginning of the array. Push,shift: An entry pops up at the beginning of the array. You can also use Unshift to insert an entry at the beginning of the array, and a Pop method that pops an item at the end of the array.

    • Sort by:Reverse: Reverse the order of the original array. Sort: Passes in a comparison function to reorder the array. The comparison function passes two parameters, the first argument is the previous item, the second argument is the latter, and if the first argument should precede the second argument, a negative number is returned, or zero if the two arguments are equal, or a positive number if the first argument should be after the second argument.

    • How to:concat: Creates a new array based on the current array and incoming parameters. If the parameter is an array, each of its items is added to the current array, and if it is not an array, it is added to the end of the current array.

var a = [1, 2, 3];var B = A.concat (4, [5, 6]); Console.log (b); 1,2,3,4,5,6

Slice: Creates an array based on one or more items of the current array. Slice can pass in one or two parameters, that is, the starting and ending positions of the returned items in the original array. If only one parameter is passed in, it returns all items at the specified position to the end of the current array. If you pass in two parameters, all items that return the starting position specified by the first parameter to the end position (excluding the end position entry) of the second parameter are defined. Slice is not affected by the original array Oh.

var a = [1, 2, 3, 4, 5];var B = A.slice (2); var c = A.slice (2, 3); Console.log (b);     3,4,5console.log (c); 2

Splice: The most complex method in the array method, splice needs to pass in two or more than two arguments, the first parameter is the starting position of the deleted or inserted item, the second parameter is the deletion of several items, the third argument and the following parameter represents the inserted item. Splice will modify the original array, and the return value is the deleted item.

var a = [1, 2, 3, 4, 5, 6];a.splice (0, 2);           [3,4,5,6]a.splice (1, 0, 7, 8);           [1,7,8,2,3,4,5,6]a.splice (1, 2, 7, 8); [1,7,8,4,5,6]

ECMAScript5 method to be continued ...

Methods of the Array object in JavaScript

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.