Js learning notes (8) array

Source: Internet
Author: User

1. Associating arrays isValueAndStringWhile the regular array isValueAndNon-negative integerAssociated.

2. Create an array.

1) create with constructor Array ()

Var a = new Array ();// Creates an empty array.

Var a = new Array (10 );// If there is only one numeric parameter, it indicates the length of the array. The value of each element in the array is undefined, and the length of the array is also specified to this value.

Var a = new Array ("hello", "world ")// If multiple parameters exist, it indicates the first n elements of the array.

2) directly create an array

Var a = [];// Creates an empty array.

Var a = ["hello", "world"]// An array with elements is created.

 

3. The subscript of the js array starts from 0.

The subscript of the js array must be an integer greater than or equal to 0 and less than the power of 2 minus 1. If the number used is too large or uses a negative number, floating point number (or Boolean value, object, or other value), js will convert it into a string, use the generated string as the name of the object attribute, instead of the subscript of the array. At this time, it is no longer a regular array element, but an object attribute.

Js can change the length of the array at any time. For example, to add an array element, you only need to assign a value to it using the new array subscript.

Arrays in js are sparse, that is, the subscript of an array does not need to fall into a continuous number range. Only existing array elements are allocated to the memory.

 

4. The length attribute of the array can be read or written. If a value smaller than the current value is set for the length attribute, the array will be truncated, array elements beyond the length will be discarded. If the length value is greater than the current value, the new, defined array element will be added to the end of the array to reach the length of the newly specified array. The array can also use a string as the key name. However, after an array element is added with a string key name, the length of the array does not seem to change.

5. array method ()

1,Join () methodYou can convert all elements of an array into strings and connect them. You can specify a string parameter as a connector. If no parameter is input, the default connector is comma. For example:

Var a = [1, 2, 3];

Var a_string = a. join (); // The value is 1, 2, and 3 (string type)

Var a_string = a. join ("*"); // The value is 1*2*3 (string type)

 2. reverse () methodReturns the inverted array element order to the inverted array. This array is operated on the basis of the original array, which is equivalent to modifying the original array, it does not create a new array, such

Var a = [1, 2, 3];

A. reserve (); // array a has changed to [3, 2, 1]

3. concat () methodTo add any element to the array. The parameter is the new array element to be added. If the parameter is an array, the array is expanded into elements and added to the original array, but up to one layer can be expanded, as shown in figure

Var a = [1, 2, 3];

Var a = a. concat (); // At this time, a changes to [, 5]

Var a = a. concat ([]) // The result is [, 5].

Var a = a. concat (4, [5, [6, 7]) // The result is [1, 2, 3, 4, 5, [6, 7].

4. slice () methodReturns a part of the specified array. Its first parameter specifies the elements of the array fragment starting from the array,The second parameter specifies that the part ends with the elements of the array, but does not contain the elements at this position., 0 indicates the first element. If there is only one parameter, it indicates starting from the position specified by this parameter to all the remaining elements of the array. If the parameter has a negative number, it indicates the last element of the array. For example,-1 indicates the last element of the array, and-4 indicates the last 4th elements of the array.

Var a = [1, 2, 3, 4, 5, 6];

A. slice () // return [, 3]

A. slice (3) // return [, 6]

A. slice (1,-1) // return [2, 3, 4, 5]

A. slice (-3,-2) // return [4]

 

5. splice () methodYou can delete array elements or add array elements.

The first parameter is the starting position of the array element to be deleted, and the second parameter is the length of the array element to be deleted (note that the second parameter of the slice method is not the length ), if there is only one parameter, all array elements starting from this parameter are deleted. The first two parameters of splice specify the array elements to be deleted, and more parameters can be provided to indicate the new array elements to be inserted, the insert position is specified by the first parameter. Note that the splice () method modifies the original array. This method returns an array composed of the deleted array elements.

Var a = [1, 2, 3, 4, 5, 6, 7, 8]

A. splice (4) // return [,]. At this time, a is [,].

A. splice () // return [2, 3]. At this time, a is [].

A. splice () // returns an empty array. At this time, a is [].

Var a = [1, 2, 3, 4, 5]

A. splice (, "a", "B") // returns an empty array. In this case, a is [, "a", "B", 5].

A. splice (, [], 3) // return ["a", "B"]. At this time, a is [, [],].

The splice () method is different from the concat method. The inserted array is not expanded.

6. push () and pop () Methods

Push () is used to add one or more elements to the array. The returned value is the length of the modified array,Note that it is different from the concat () method (the concat method returns a new array without modifying the original array );The pop () method is used to delete the last element of an array without parameters. The value of the element it deletes is returned.Both the push () and pop () Methods modify the original array.

Var a = [];

A. push (); // returns 2. In this case, a is [].

A. pop (); // returns 2. In this case, a is [1].

A. push (3); // returns 2. In this case, a is [1, 3].

A. pop (); // 3 is returned. In this case, a is [1].

A. push ([]) // returns 2. At this time, a is [1, [].

7. unshift () and shift () Methods

The unshift () and shift () methods are similar to the push () and pop () methods, except that the elements are inserted or deleted before the array.

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.