Javascript Study Notes (5) Array type

Source: Internet
Author: User
First Type of array creation: 1 varcolorsnewArray (); 2 varcolorsnewArray (20); // create an array containing 20 items 3 varcolorsnewArray (& quot; Greg & quot ;); // create an array 4varcolors containing 1 string & quot; Greg & quot...

Array Creation
First:

1 var colors = new Array ();
2 var colors = new Array (20); // create an Array containing 20 items
3 var colors = new Array ("Greg"); // create an Array containing 1 item, that is, the string "Greg"
4 var colors = new Array ("red", "blue", "green"); // create the following three items:

1 var colors = ["red", "blue", "green"];
2 var colors = []; // create an empty array. Note: The index of the array starts from 0.

1. length attribute
The number of items in the array stored in the length attribute, for example:

1 var colors = ["red", "blue", "green"];
2 alert (colors. length); // The 3 length attribute is not read-only. You can use the length attribute to remove an item at the end of the array or add a new item, such:

1 var colors = ["red", "blue", "green"];
2 colors. length = 2;
3 alert (colors); // red, blue
4 colors [colors. length] = "black ";
5 alert (colors); // red, blue, black2.join () method, connected to items in the array

1 var colors = ["red", "blue", "green"];
2 alert (colors. join (","); // red, blue, green
3 alert (colors. join ("|"); // red | blue | the stack method of the green3. array: push () and pop ()
The push () method can accept any number of parameters to add them to the end of the array one by one, and return the length of the modified array.
The pop () method removes the last entry from the end of the array, reduces the length value of the array, and returns the removed entry.


1 var colors = new Arrary (); // create an array
2 var count = colors. push ("red", "green"); // push two entries to the end of the array.
3 alert (count); // 2
4 count = colors. push ("black"); // push an item to the end of the array.
5 alert (count); // 3
6 var item = colors. pop (); // remove the last item and return this value
7 alert (item); // "black"
8 alert (count); // 2
4. array queue Methods: push (), shift (), unshift ()
The push () method is the same as above.
The shift () method removes the first entry from the array and returns this entry. The length of the array is reduced by 1.
The unshift () method adds any item to the front of the array and returns the length of the new array.


Var colors = new Arrary (); // create an array
Var count = colors. push ("red", "green"); // push two entries to the end of the array.
Alert (count); // 2
Count = colors. push ("black"); // push an item to the end of the array.
Alert (count); // 3
Var item = colors. shift (); // remove the first item and return this value.
Alert (item); // "red"
Alert (colors); // green, black
Count = colors. unshift ("blue"); // push an item to the front end of the array
Alert (count); // 3
Alert (colors); // blue, green, black
5. Re-sorting method: reverse () and sort ()
The reverse () method reverses the order of array items www.2cto.com
By default, the sort () method arranges array items in ascending order of string size. You can use a function of the same size as a parameter.

1 var values = [1, 2, 3, 4, 5];
2 values. reverse ();
3 alert (values); // 5, 4, 3, 2, 1
1 // ascending sort function
2 function compare (value1, value2 ){
3 if (value1 <value2 ){
4 return-1; // change the descending order to 1.
5} else if (value1> value2 ){
6 return 1; // change the descending order to-1.
7} else {
8 return 0;
9}
10}
1 // array in ascending order
2 var values = [0, 1, 5, 15, 20, 10];
3 values. sort (compare );
4 alert (values); // 15,201, // this function can be used for numeric types in ascending order.
2 function compare (value1, value2 ){
3 return value2-value1;
4} 6. array Methods: concat () method, slice () method, and splice () method
The concat () method adds parameters to the end of the original array and returns a new array. The original array remains unchanged.
The slice () method returns the items in the array. When a parameter is set, all the items from the specified position to the end of the array are returned. When two parameters are set, the items between the start position and the end position are returned (excluding the end position ), original array unchanged
The splice () method inserts, deletes, or replaces items in the array, and returns the deleted items (an empty array is returned if no deletion is performed). The original array changes.

1 // concat () method
2 var colors = ["red", "green", "blue"];
3 var colors2 = colors. concat ("yellow", ["black", "brown"]);
4 alert (colors); // red, green, blue
5 alert (colors2); // red, green, blue, yellow, black, brown
1 // slice () method
2 var colors = ["red", "green", "blue", "yellow", "black"];
3 var colors2 = colors. slice (1); // when a parameter is set, all the items from the specified position to the end of the array are returned.
4 var colors3 = colors. slice (); // return the entry between the start position and the end position (excluding the end position) When two parameters are set)
5 alert (colors2); // green, blue, yellow, black
6 alert (colors3); // green, blue, yellow

1 // splice () method
2 // insert items. Three parameters are specified during insertion: Start position, 0 (items to be deleted), and items to be inserted.
3 var colors = ["red", "green", "blue"];
4 var inserted = colors. splice (, "yellow", "orange"); // insert two items from position 1
5 alert (colors); // red, yellow, orange, green, blue
6 alert (inserted); // empty array
7
8 // replacement item. Three parameters are specified during deletion: Start position, items to be deleted, and any items to be inserted.
9 var colors = ["red", "green", "blue"];
10 var replaced = colors. splice (, "black", "brown"); // delete one item and insert two items
11 alert (colors); // red, black, browm, blue
12 alert (replaced); // green

 

 

From Sunday walk

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.