Summary of JavaScript Array Functions

Source: Internet
Author: User
Tags javascript array

Connection and division of array data items:

Var mya = new Array ();

Mya [0] = "aa ";

Mya [1] = "bb ";

Mya [2] = "cc ";

Mya [3] = "dd ";

Function joinArray ()...{

Var myText = mya. join (",");

Alert (myText );

}

Function splitArray ()...{

Var mya2 = myText. split (",");

Alert (mya2 [I]);

}

 

The array. concat () method can be used to concatenate two array objects into a new array object. For example:

Var array1 = new Array (1, 2, 3 );

Var array2 = new Array ("a", "B", "c ");

Var array3 = array1.concat (array2 );

// Result: array3 with values 1, 2, 3, "a", "B", "c"


Var arrayText = myArray. join (",");

 

STACK:

Array. pop ()

Array. push (valueOrObject)

Array. shift ()

Array. unshift (valueOrObject)

Push () and pop () work at the end of the array, shift () and unshift () work at the front end of the array.

When a value is pushed () to an array, the value is appended to the array as the last data item.

When the array. pop () method is used, the last entry in the array is removed from the stack and returned as the return value. The length of the array is reduced by 1.

When an unshift () value is added to an array, the value is inserted into the array as the first data item.

When the array. shift () method is used, the first item in the array is removed from the stack and returned as the return value. At the same time, the length of the array is reduced by 1.

 

Array. reverse () --> return value: array entry in the opposite order of the original array

Array. slice (startIndex [, endIndex]) --> return value: array

This method requires a parameter, that is, the starting index location for extraction. If the second parameter is not specified, the extraction continues until the end of the array,

Otherwise, only the location specified by the second parameter is extracted, excluding that location.

 

Array. splice (startIndex, deleteCount [, item1 [, item2 [,... itemN])

If you need to delete some items from the array, you can use the array. splice () method to simplify this task. Otherwise, you need to combine a new array from the selection items of the original array.

The first of the two parameters is an Index Integer Based on 0, which points to the first item deleted in the current array;

The second parameter is also an integer, indicating the number of consecutive items to be deleted from the array. Deleting an array item affects the length of the array. The deleted item is returned by the splice () method as its own array.

You can also use the splice () method to replace array items. The third parameter is an optional parameter, which allows you to provide the data elements of the array to be inserted. These data elements must replace the deleted data items.

The added data items can be any JavaScript data type. The number of new data items is not necessarily equal to the number of deleted data items.

In fact, by specifying the second parameter as 0, you can use splice () to insert one or more data items to any position of the data.

 

Array. toLocaleString () --> generally, each item in the array is separated by a comma.

Array. toString () --> the exact string conversion of this method depends on the implementation method of a specific browser.

Generally, the old method array. toString () with stronger container strength is used ()

 

Array. sort ([compareFunction]) --> return value: array items in the order controlled by the compareFunction Algorithm

The sorting order of the original array is changed!

By default, sorting is performed based on the ASCII value of characters.

Return Value of the comparison function:

<0: Value B is placed behind

= 0: the order of a and B does not change.

> 0: Value a is placed after value B.

For example, sort by number

 

MyArray = new Array (2,123)

Function compare (a, B )...{

Return a-B

}

MyArray. sort (compare)

 

 

For example, compare the last character of each data item in the string array and sort it alphabetically.

 

Function compare (a, B )...{

// Last character of array strings

Var aComp = a. charAt (a. length-1)

Var bComp = B. charAt (B. length-1)

If (aComp <bComp)... {return-1}

If (aComp> bComp)... {return 1}

Return 0

}

 

 

For example, if an array contains such objects, their property values define information about employees, one of which is the age of an employee and is defined as a string type.

The following comparison function is used to sort the Array Based on the value corresponding to the age attribute of the object.

 

Function compare (a, B )...{

Return parseInt (a. age)-parseInt (B. age)

}

 

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.