JavaScript authoritative Guide (6th edition) Learning Note Four

Source: Internet
Author: User
Tags array length

Seventh Chapter Array

First, create an array

Using the array direct amount is the simplest method:

var empty=[]; var primes=[1,3,5,7,11]; var m=[1.1, "a",true]; var b=1024; var table=[b,b+1,b+2]; var bc=[[1,{x:1,y:2}],[2,{x:3}]]// If a value is omitted from the direct amount, the undefined value is assigned by default var count=[1,,3];

The allowed elements at the end of an array's direct amount are comma:

var a=[1,2,];   // two elements are 1 and 2, respectively var b=[True,,];  // Two elements are true and undefined, respectively

Another way is to call the constructor array ():

var arra=New// parameterless var arrb=New Array (ten);  // Create an array of length 10 var a=New Array (5,3,1, "testabc");  // explicitly specifying an element

Ii. reading and writing of array elements

var a=["World"; var value=a[0];  // read the No. 0 element of a[1]=3.14;  // Write a 1th element i=2; a[i]=3;  // Write a 2nd element a[i+1]= "Hello";   // Write a 3rd element a[a[i]]=a[0];   // Read the No. 0 and 2nd elements and write the 3rd element

Arrays are special forms of objects, and using square brackets to access array elements is like accessing an object's properties in square brackets-that is, JavaScript converts the numeric index of an array into a string: that is, the index value 0 becomes "0", the index value 1 becomes "1", and then it is used as the property name. This can also be done for regular objects:

var o={};  // create a Common object o[1]= "one";   

Although described above, the special thing about arrays is that you can automatically maintain the value of the length property (when the length is less than 2^32). All indexes are property names, but only the attribute names of integers between 0~ (2^32-2) can be indexed. All arrays are objects

Third, sparse array

A sparse array is an array that contains a discontinuous index starting at 0, so the length property value of a sparse array is greater than the actual number of elements.

a=New Array (5);    // the array has no elements, but the length is 5a=[];        // create an empty array, length=0a[1000]=0;    // adds an element to array a, at which point the length=1001

Four, array length

When you set the array length property value to an integer value that is less than the current length, the array changes accordingly.

a=[1,2,3,4,5];a.length=3;   // at this point a becomes [a.length=0];   // at this point a becomes []a.length=5;   // at this point a length of 5, but no element, similar to the new Array (5)

If you set the value of the length property to an integer value greater than the current length, this does not add an element to the array, but only creates an empty area at the end of the original array.

V. Additions and deletions of array elements

To assign a value directly to the new index:

A=[];a[0]= "Zero"; a[1]= "one";

You can also add one or more elements to the tail by using the push () method, which is an effect when you add an element and assign a value to an array A[a.length]:

A=[];a.push ("Zero"); A.push ("One", "one");    // at this point A is ["zero", "one", "both"]

If you want to insert an element in the header of an array, you can use the Unshift () method, and the other elements move to the higher index position in turn.

a=[1,2,3]; delete a[1];   // at this point there are no elements at index 1, and a becomes a sparse array  in A;      // falsea.length    // is still 3, length unchanged. 

You can also set the length to be small to remove the trailing elements. The array also has the pop () method, which reduces the length by 1 per use and returns the value of the element being deleted. There is also a shift () method that removes an element from the head.

VI. Array traversal

Using a For loop is the most common way to iterate through an array:

var keys=object.keys (o);   // get an array of O object property names var values=[];  for (var i=0; i<keys.length;i++) {  var key=Keys[i];  Values[i]=O[key];}

Seven, multidimensional arrays

var table=New Array (ten);  for (var i=0;i<table.length;i++)     table[i]=new Array (ten);  for (var row=0;row<table.length;row++) {    for (col=0;col<table[row].length ; col++) {        Table[row][col]=row*col;    }}

Eight, array method

1. Join () converts all elements in the array into strings and links together, returning the last generated string. The default is delimited by commas, or you can specify a parameter as a delimiter.

var a=[1,2,3];a.join ();    // "a.join" ("");   // " 1 2 3"A.join ("");   // "123" var b=New Array,b.join ("-");   // "---------"; 9 hyphens

2. The reverse () method returns the elements in the array in reverse order, returning the array after the inverse, replacing the original array.

var a=[1,2,3];a.reverse (). join ();     // "3,2,1", a also became [3,2,1]

3. The sort () method sorts the elements of an array according to certain rules, and when there are no parameters, the elements are sorted in alphabetical order. The undefined element in the array is queued to the tail of the array.

var a=New Array ("banana", "cherry", "apple"); A.sort (); var s=a.join (",");    // "Apple, banana, cherry"

4. The slice () method returns a fragment or sub-array of the specified array.

var a=[1,2,3,4,5];a.slice (0,3);   // return to [A.slice](3);   // return [4,5]a.slice (1,-1);   // return [2,3,4]a.slice ( -3,-2);   // back [3]

5. The splice () method can remove an element from an array, insert an element into an array, or two operations at a time. The first parameter specifies the starting position of the insertion or deletion, the second parameter specifies the number of elements that should be removed from the array, and if the second argument is omitted, all elements are deleted from the starting point to the end of the array. Splice () returns an array of deleted elements that return an empty array if no elements are deleted.

var a=[1,2,3,4,5,6,7,8];a.splice (4);    // return [5,6,7,8],a to [1,2,3,4] // return [2,3],a to [1,4] // return [4],a to [1]

Splice () The first two parameters specify the array elements that need to be deleted, followed by any parameters that specify the elements that need to be inserted into the array, and the inserted position is specified by the first parameter.

var a=[1,2,3,4,5];a.splice (2,0, ' A ', ' B ');  // return [];a into [, ' A ', ' B ', 3,4,5]a.splice (2,2,[1,2],3);   // return [' A ', ' B '];a into [1,2,[1,2],3,3,4,5]

6. Push ()/pop () Method: Push () adds one or more elements at the end of the array and returns the new length of the array. Pop () deletes the last element of the array, reduces the length of the array, and returns the value that was deleted by it. Two methods all modify and replace the original array.

JavaScript authoritative Guide (6th edition) Learning Note Four

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.