Nine local objects of Reading Notes in JavaScript advanced programming Array

Source: Internet
Author: User

Create an Array object
Copy codeThe Code is 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 codeThe Code is 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 codeThe Code is 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: append an array
Copy codeThe Code is 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 specific items
Copy codeThe Code is 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 the stack, Array provides the push and pop methods. The push method is used to add one or more items at the end of Array. pop is used to delete the last Array item and return it as the function value.
Copy codeThe Code is 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: Delete the first item in the array and use it as the return value of the function. unshift: place an item in the first position of the array, and move the remaining items downward.
Copy codeThe Code is 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 array items. sort: sort the values of array items in ascending order (first, call the toString () method to convert all values into strings)
Copy codeThe Code is as follows:
Var aColors = ["blue", "green", "red"];
AColors. reverse ();
Alert (aColors); // outputs ["red", "green", "blue"]
AColors. sort ();
Alert (aColors); // outputs ["blue", "green", "red"]

Note:
Copy codeThe Code is as follows:
Var aColors = [3, 32, 2, 5];
AColors. sort ();
Alert (aColors); // outputs [2, 3, 32, 5]

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

Splice
Splice: insert data items into the middle of the array

1. Used for deletion: If two parameters are declared, the first parameter is the location of the first item to be deleted, and the second parameter is the number of items to be deleted.
Copy codeThe Code is as follows:
Var aColors = ["red", "green", "blue", "yellow"];
AColors. splice (0, 2 );
Alert (aColors); // outputs ["blue", "yellow"]

2. Used for insertion: When three or more parameters are declared (the second parameter is 0), the data can be inserted at the specified location. The first parameter is the start location, and the second parameter is 0, the third and above parameters are the inserted items.
Copy codeThe Code is as follows:
Var aColors = ["red", "green", "blue", "yellow"];
AColors. splice (2, 0, "black", "white ");
Alert (aColors); // outputs ["red", "green", "black", "white", "blue", "yellow"]

3. Used for deletion and insertion: If three or more parameters are declared (the second parameter is not 0), the data can be inserted at the specified location. The first parameter is the start location, the second parameter is the number of items to be deleted, and the third and above parameters are the inserted items.
Copy codeThe Code is as follows:
Var aColors = ["red", "green", "blue", "yellow"];
AColors. splice (2, 1, "black", "white ");
Alert (aColors); // outputs ["red", "green", "black", "white", "yellow"]

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.