Javascript array attributes and Methods

Source: Internet
Author: User

Array can be either a string or a number. The attributes and methods provided in array are as follows:

Initialize an array
VaR arr = new array (); Create an empty array
VaR arr1 = new array (5); Create an array with a length of 5
VaR arr2 = new array (,); Create an array with values
VaR arr3 = [3, 4, 5]; a simple new array

A. Length attribute, returns the length of the array. Note that attributes are not followed by (), and attributes are followed ()

A. Push (n) inserts N at the end and returns the length of the array. You can add multiple numbers.
A. Pop () removes the last element of the array and returns this element.
A. Shift () removes the first element of the array and returns this element.
A. unshift (n) inserts N into the beginning of the array and returns the array. you can insert multiple

A. Reverse () reverses the array. Returns the inverted array. The original array value is also reversed.
A. Concat (A1) connects to an array and connects A1 to A. Multiple Arrays can be connected.
A. Slice (n, m) returns n ~ Child array of M, which can be left empty

A. splice (START, N, Item1, item2..) deletes n digits from start and inserts the following elements into the position. Optional. the return value is the deleted N-bit.
A. Join ("-") concatenates all elements in array a with the symbol in parentheses. For example, var a = [3, 4, 5]; returns 3-4-5
A. tostring ()
A. valueof () both return strings that use commas to connect to array elements.
A. Sort () in ascending order. This sorting is based on Unicode
A. Sort (sortfunction) needs to fill in parameters when using numerical sorting
Function sortfunction (A, B) is in ascending order, and return B-A is in descending order.
{Return a-B ;}

 

Next we will provide some functions

1. Output an array

Function $ (STR) {return document. write (STR)} function showarray (A0) // output Array {for (VAR I = 0; I <a0.length; I ++) {document. write (A0 [I] + "");} document. write ("</BR> ");}

2. Guess what the value of arr3 is at this time?

var arr2 = [34,23,12,23];var arr3 = arr2.concat(arr2.reverse(),arr2.reverse().push(3,4));

Analysis: we transferred arr2 once, and then again, so arr2 is still in the original order. Then I added two numbers to it, and arr2 changed to 34,23, 12,23, 3,4.

Arr3 is connected to two such arr2 instances. However, the length of the array returned by push () is not complete yet. At this time, the length of arr2 is 6, so a 6 is added at the end. You guessed it?

These methods of array can implement many functions and write them when I find them.

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.