JavaScript Learning Notes (v) Array Arrays Type Introduction _ Basics

Source: Internet
Author: User
Creation of arrays
First type:
Copy Code code 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 that contains 1 entries, the string "Greg"
var colors = new Array ("Red", "Blue", "green"); Create a 3 item that contains

The second type:
Copy Code code as follows:

var colors = ["Red", "Blue", "green"];
var colors = [];//creates an empty array

Note: The index of an array starts at 0

1. Length Property
The number of items in the length property that holds the array, such as:
Copy Code code as follows:

var colors = ["Red", "Blue", "green"];
alert (colors.length); 3

The Length property is not read-only, you can use the Length property to remove an item at the end of the array, or to add a new item, such as:
Copy Code code 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, connecting an item in an array
Copy Code code as follows:

var colors = ["Red", "Blue", "green"];
Alert (Colors.join (",")); Red,blue,green
Alert (Colors.join ("| |)"); red| | blue| | Green

3. Stack method for arrays: Push () and pop ()
The push () method can accept any number of arguments to add the end of the array individually, and returns the length of the modified array
The Pop () method removes the last item from the end of the array, reducing the length value of the array, and returning the removed item
Copy Code code as follows:

var colors = new Arrary (); Create an array
var count = Colors.push ("Red", "green"); Push two items to the end of the array
alert (count); 2
Count = Colors.push ("Black"); Push one entry to the end of the array
alert (count); 3
var item = Colors.pop (); Remove the last item and return the value
alert (item); "Black"
alert (count); 2

4. Array Queue method: Push () and shift (), Unshift ()
Push () Method ditto
The shift () method moves the first item in the array and returns the item, which 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 Code code as follows:

var colors = new Arrary (); Create an array
var count = Colors.push ("Red", "green"); Push two items to the end of the array
alert (count); 2
Count = Colors.push ("Black"); Push one entry to the end of the array
alert (count); 3
var item = Colors.shift (); Remove the first item and return the value
alert (item); "Red"
alert (colors); Green,black
Count = Colors.unshift ("Blue"); Push one entry to the front of the array
alert (count); 3
alert (colors); Blue,green,black

5. Reordering methods: Reverse () and sort ()
Reverse () method reverses the order of array items
The sort () method arranges the array items by default in ascending order of string size and can accept a function of the size as an argument
Copy Code code as follows:

var values = [1,2,3,4,5];
Values.reverse ();
alert (values); 5,4,3,2,1

Copy Code code as follows:

Ascending sort function
function Compare (value1,value2) {
if (value1 < value2) {
return-1; Change in descending to 1
else if (value1 > value2) {
return 1; Change in descending to-1
} else {
return 0;
}
}

Copy Code code as follows:

Array in ascending order
var values = [0,1,5,15,20,10];
Values.sort (Compare);
alert (values);//0,1,5,10,15,20

Copy Code code as follows:

For numeric types you can use this function, ascending
function Compare (value1,value2) {
return value2-value1;
}

6. Some methods of array: Concat () method, slice () method and Splice () method
The concat () method adds a parameter to the end of the original array, returns the new array, and the original array is unchanged
The slice () method returns an item in an array that returns all items at the specified position to the end of the array, with two arguments returning the entry between the start position and the end position (excluding the end position), and the original array is unchanged
The splice () method inserts, deletes, or replaces an item in an array, returns the deleted item (an empty array is returned without deletion), and the original array changes
Copy Code code 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 Code code as follows:

Slice () method
var colors = ["Red", "green", "blue", "yellow", "black"];
var colors2 = Colors.slice (1); Returns all items at the end of the array at the specified position in a parameter
var colors3 = Colors.slice (1,4); Returns the entry between the start position and the end position (excluding the end position) when two parameters
alert (COLORS2); Green,blue,yellow,black
alert (COLORS3); Green,,blue,yellow

Copy Code code as follows:

//splice () method
//Insert entry, specify 3 parameters when inserting: Start position, 0 (item to delete), item to insert
var colors = ["Re D "," green "," blue "];
var inserted = Colors.splice (1,0, "Yellow", "orange");//Insert two items starting at position 1
alert (colors); Blue
Alert (inserted);//empty array

//substitution, 3 parameters specified when deleting: Start position, item to delete, any item to insert
var colors = ["Red", "green", "blue"] ;
var replaced = Colors.splice (1,1, Black, brown);//delete one, insert two
alert (colors);//red,black,browm,blue
Aler T (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.