JavaScript Array Method Summary

Source: Internet
Author: User
Tags javascript array

There are two basic ways that JavaScript creates arrays. The first is the use of the array constructor.

var colors = new Array ();

var colors = new Array (20);

var colors = new Array ("Red", "Blue", "green");

Another way is to omit the new symbol:

var colors = Array (3);

var colors = Array ("Greg");

1. Detecting arrays

if (value instanceof Array) {}

if (Array.isarray (value)) {}

2. Conversion method

Tolocalstring () converts the array to a local array and returns the result.

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

ValueOf () returns the original value of the array object.

Join () Places all elements of an array into a string. element is delimited by the specified delimiter.

3. Stack method

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

Pop () deletes and returns the last element of the array.

4. Queue method

Shift () deletes and returns the first element of the array.

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

5. Reorder Methods

Reverse () Reverses the order of the elements in the array.

Sort () Sorts the elements of an array.

6. How to Operate

Concat () joins multiple arrays.

Slice () Creates a new array based on one or more items in the current array.

Splice () Inserts an item into the middle of the array.

7. Location method

IndexOf () looks backwards from the beginning of the array.

LastIndexOf () looks forward from the end of the array.

8. Iterative Methods

Every () runs the given function for each item in the array, and returns True if every item of the function returns True.

The filter () runs the given function for each item in the array, returning the new array of items that the function returns as true.

ForEach () runs the given function for each item in the array. This method has no return value.

Map () runs the given function for each item in the array, returning a new array that consists of the results of each function call.

Some () runs the given function for each item in the array, and returns True if the function returns true for either item.

9. Merge method

ECMAScript5 New Method:

Reduce () iterates over each item in an algebraic group, and then builds the final return value. Start with the first item.

Reduceright () iterates over each item in an algebraic group, and then builds the final return value. Start with the last item.

~ ~ ~ Night Plus Example ~ ~

JavaScript Array Method Summary

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.