JavaScript Advanced Programming Reading notes nine local objects Array_javascript tips

Source: Internet
Author: User
Create an Array object
Copy Code code as follows:

One
var avalues=new Array ();

Two
var avalues=new Array (20);

Three
var acolors=new Array ();
acolors[0]= "Red";
Acolors[1]= "Green";
Acolors[2]= "Blue";

Four
var acolors=new Array ("Red", "green", "blue");

Five
var acolors=["Red", "green", "blue"];

Join && Split
Join: Connection string
Copy Code code as follows:

var acolors=["Red", "green", "blue"];
Alert (Acolors.join (","));//outputs "Red,green,blue"
Alert (Acolors.join ("-spring-"));//outputs "Red-spring-green-spring-blue"
Alert (Acolors.join ("["));//outputs "Red][green][blue"

Split: Split string
Copy Code code as follows:

var scolors= "Red,green,blue";
var acolors=scolors.split (",");//outputs ["Red", "green", "blue"]
var redcolors=acolors[0].split ("");//outputs ["R", "E", "D"]

Concat && Slice
Concat: Appending array
Copy Code code as follows:

var acolors=["Red", "green", "blue"];
var acolors2=acolors.concat ("Yellow", "purple");
alert (acolors);//outputs ["Red", "green", "blue"]
alert (aColors2);//outputs ["Red", "green", "blue", "yellow", "purple"]

Slice: Returns a new array with a specific item
Copy Code code as follows:

var acolors=["Red", "green", "blue", "yellow", "purple"];
var acolors2=acolors.slice (1);//outputs ["Green", "blue", "yellow", "purple"]
var acolors3=acolors.slice (1,4);//outputs ["Green", "blue", "yellow"]

Push && Pop
Like stacks, array provides a push and pop method that is used to add one or more items at the end of the array, and POPs to delete the last array item and return it as a function value
Copy Code code as follows:

var stack=new Array;
Stack.push ("Red");
Stack.push ("green");
Stack.push ("Blue");
alert (stack);//outputs ["Red", "green", "blue"]
var vitem=stack.pop ();
alert (vitem);//outputs ["Blue"]
alert (stack);//otputs ["Red", "green"]

Shift && Unshift
Shift: Deletes the first item in the array as a function return value, Unshift: Place an item in the first position of the array, and then move the remaining items down one position
Copy Code code as follows:

var acolors=["Red", "green", "blue"];
var vitem=acolors.shift ();
alert (acolors);//outputs ["Green", "blue"]
alert (vitem);//outputs ["Red"]
Acolors.unshift ("Black");
alert (acolors);//outputs ["Black", "green", "blue"]

Reverse && Sort
Reverse: Reverse the order of the array items, sort: By the value of the array items in ascending order (first call the ToString () method, convert all values to strings)
Copy Code code as follows:

var acolors=["Blue", "green", "Red"];
Acolors.reverse ();
alert (acolors);//outputs ["Red", "green", "blue"]
Acolors.sort ();
alert (acolors);//outputs ["Blue", "green", "red"]

Attention:
Copy Code code as follows:

var acolors=[3,32,2,5];
Acolors.sort ();
alert (acolors);//outputs [2,3,32,5]

This is because the numbers are converted to strings and then compared by character code.

Splice
Splice: Inserts the data item into the middle of the array

1, for deletion: Just declare two parameters, the first parameter is the position of the first item to be deleted, and the second parameter is the number of deleted items
Copy Code code as follows:

var acolors=["Red", "green", "blue", "yellow"];
Acolors.splice (0,2);
alert (acolors);//outputs ["Blue", "yellow"]

2, used as an insert: Declare three or more parameters (the second parameter is 0) you can insert data into the specified position, the first parameter is the ground start position, the second parameter is 0, and the third and above parameters are inserted
Copy Code code as follows:

var acolors=["Red", "green", "blue", "yellow"];
Acolors.splice (2,0, "Black" and "white");
alert (acolors);//outputs ["Red", "green", "black", "white", "blue", "yellow"]

3, used as delete and insert: Declare three or more parameters (the second parameter is not 0) you can insert data into the specified position, the first parameter is the location, the second parameter is the number of items to delete, and the third and above parameters are inserted
Copy Code code as follows:

var acolors=["Red", "green", "blue", "yellow"];
Acolors.splice (2,1, "Black" and "white");
Alert (acolors);//outputs ["Red", "green", "black", "white", "yellow"]

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.