A common array method for JS

Source: Internet
Author: User

1. Basic methods for creating arrays:

1.1Empty Arrayvar obj=new Array ();
1.2specifying an array of lengthsvar obj=new Array (size);
1.3specifying an array of elementsvar obj=new Array (Elements1,Elements2,...,Elementsn);
1.4single-Dimension arrayvar obj=[Elements1,Elements2,Elements3,...,Elementsn];
1.5Multidimensional Arraysvar a=new Array ([Array Sequence1],[Array Sequence2],[Array SequenceN]);

2. Use the literal representation method:

Represented by a pair of square expansions [] that contain array items, separated by commas between multiple arrays.

Syntax: Array.Length  //Array length

function: Get array length, return value is number

1. By setting length you can remove an item from the end of the array or add a new item to the array.

2. When a value exceeds the current array size, the array opportunity recalculates its length, and the length value equals the index value of the last item.

3. Properties of an array
Properties Description

Constructor

Returns a reference to the array function that created this object.
Length Sets or returns the number of elements in the array.

Prototype

gives you the ability to add properties and methods to an object.
4. Methods for array Objects

4.1 Adding attributes:

1.push (): Adds an array to the end of the original array and returns the length of the array.

var A=[1,2,3]var b = A.push (5,6)     //b = [1,2,3,5,6]

2.unshift () adds a parameter to the beginning of the original array and returns the length of the array.

var a = [1,2,3];var B = A.unshift ( -2,-1);     b = [ -2,-1,1,2,3]

3.concat (): Merge two arrays

4.2 Delete attributes:

 1.pop (): Deletes the last item of the original array, returns the value of the deleted element, and returns undefined if the array is empty.

var a = [1,2,3,4]var B = A.pop ();    b = [[+]

2.shift (): Deletes the first item of the original array, returns the value of the deleted element, and returns undefined if the array is empty.

var a = [1,2,3]var B = a.shift ();   b = [2,3]
4.3 Word Group:

  1.splice (): the position of the first parameter to manipulate the second argument starts at this position the number of actions to manipulate the third argument What do you want to replace with any item you delete?

2.slice (Start,end): Returns the selected element from an existing array

Start: Specifies where to start the selection, and if it is negative, it specifies that the position is calculated from the end of the array.

End: (optional) specifies where to end the selection, which is an array subscript at the end of the array fragment.

features
if it is negative, the position is determined with the array length plus the value
actual subscript for an array from the starting position
the actual subscript for the end position is the end value minus 1

3.join (): Used to put all the elements in the array into a string;

4.4 Array Sorting:

  1.Reverse (): Use and reverse the order in the array.

2.sort (): Used to sort the elements in the array.

2.1: Even if each item in the array is an array, the Sort method compares the string.

The 2.2:sort () method can accept a comparison function as a parameter.

4.5 Conversion of arrays:

1.toString () is converted to a string and returned.

2.tolocalestring () is converted to a local format string and returned.

3.join () splits the array with the specified delimiter and converts to a string

5.ECMAScript 5 method:

5.1 Placement Method:

   IndexOf (): From the beginning of the array (position) to find;

Lastindeof (): Search from the end of the array;

5.2: Iterative method:

Every returns true if the function returns true for each item ;

Filter returns all array members with a value of true;

    ForEach no return value ;

Map Returns an array of results for each function call;

Some returns true if any one of The entries returns true ;

  5.3 Narrowing down Method:

    The start bit of the reduce array begins the traversal;

Reduceright begins at the end of the array

A common array method for JS

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.