Javascript Study Notes (5) Introduction to Array types

Source: Internet
Author: User

Array Creation
First: Copy codeThe Code is as follows: var colors = new Array ();
Var colors = new Array (20); // create an Array containing 20 items
Var colors = new Array ("Greg"); // create an Array containing 1 item, that is, the string "Greg"
Var colors = new Array ("red", "blue", "green"); // create three items

Second:Copy codeThe Code is as follows: var colors = ["red", "blue", "green"];
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:Copy codeThe Code is as follows: var colors = ["red", "blue", "green"];
Alert (colors. length); // 3

The length attribute is not read-only. You can use the length attribute to remove items from the end of the array or add new items, such:Copy codeThe Code is as follows: var colors = ["red", "blue", "green"];
Colors. length = 2;
Alert (colors); // red, blue
Colors [colors. length] = "black ";
Alert (colors); // red, blue, black

2. join () method, connected to items in the arrayCopy codeThe Code is as follows: var colors = ["red", "blue", "green"];
Alert (colors. join (","); // red, blue, green
Alert (colors. join ("|"); // red | blue | green

3. Stack method of 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.Copy codeCode: 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. pop (); // remove the last item and return this value
Alert (item); // "black"
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.Copy codeCode: 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.
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.Copy codeThe Code is as follows: var values = [1, 2, 3, 4, 5];
Values. reverse ();
Alert (values); // 5, 4, 3, 2, 1

Copy codeThe Code is as follows: // The ascending sorting Function
Function compare (value1, value2 ){
If (value1 <value2 ){
Return-1; // change the descending order to 1.
} Else if (value1> value2 ){
Return 1; // change the descending order to-1.
} Else {
Return 0;
}
}

Copy codeThe Code is as follows: // sort arrays in ascending order
Var values = [0, 1, 5, 15, 20, 10];
Values. sort (compare );
Alert (values); // 0, 1, 5, 10, 15, 20

Copy codeThe Code is as follows: // this function can be used for numeric type, in ascending order
Function compare (value1, value2 ){
Return value2-value1;
}

6. array Methods: concat (), slice (), and splice ()
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.Copy codeThe Code is as follows: // concat () method
Var colors = ["red", "green", "blue"];
Var colors2 = colors. concat ("yellow", ["black", "brown"]);
Alert (colors); // red, green, blue
Alert (colors2); // red, green, blue, yellow, black, brown

Copy codeThe Code is as follows: // slice () method
Var colors = ["red", "green", "blue", "yellow", "black"];
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.
Var colors3 = colors. slice (); // return the entry between the start position and the end position (excluding the end position) When two parameters are set)
Alert (colors2); // green, blue, yellow, black
Alert (colors3); // green, blue, yellow

Copy codeThe Code is as follows: // The splice () method
// Insert items. Three parameters are specified during insertion: Start position, 0 (items to be deleted), and items to be inserted.
Var colors = ["red", "green", "blue"];
Var inserted = colors. splice (, "yellow", "orange"); // insert two items from position 1
Alert (colors); // red, yellow, orange, green, blue
Alert (inserted); // empty array

// Replacement item. Three parameters are specified during deletion: Start position, items to be deleted, and any items to be inserted.
Var colors = ["red", "green", "blue"];
Var replaced = colors. splice (, "black", "brown"); // delete one item and insert two items
Alert (colors); // red, black, browm, blue
Alert (replaced); // green

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.