JavaScript basic teaching thread array Array_ basics

Source: Internet
Author: User

String, numeric, and Boolean values are discrete values (scalar), and if a variable is discrete, then it has only one value at any time.

If you want to use variables to store a set of values, you need to use arrays (array).

An array is a collection of tree values with the same name, each of which is an element of an array, and can be used by the variable team to store the names of each member of the group.

In JavaScript, an array is created using a keyword array declaration, and a coworker can declare the length of the variable. For example

Copy Code code as follows:

var ateam = new Array (12);//Declare the length of a variable

When you cannot predict the final number of arrays, you can declare an array without specifying a specific number. For example:

Copy Code code as follows:

var ateam = new Array ();/////The final number of the array is unknown, you can not declare a specific number
Ateam[0] = 1414;
ATEAM[1] = "Beijing";
ATEAM[2] = 0x4;
ATEAM[3] = "I can";
ATEAM[4] = "red";
ATEAM[5] = "Blue";
ATEAM[6] = "orange";

Alternatively, you can create an array directly

Copy Code code as follows:

var ateam = new Array ("n", "Blue", "Red", "Beijing");

As with strings, arrays can also use length to get and specify the length of an array.

Copy Code code as follows:

var ateam = new Array ("n", "Blue", "Red", "Beijing");
document.write (ateam[1]+ "<br>");
document.write (ateam.length + "<br>")

Note: A more profound understanding of arrays.

Copy Code code as follows:

var ateam = new Array ("n", "Blue", "Red", "Beijing");
ATEAM[20] = "12415"
document.write (ateam[20]+ "<br>");
document.write (ateam.length + "<br>")

Alternatively, the array can be defined with []. Use commas to separate the middle.

Copy Code code as follows:

STeam = [10, "5565", "Beijing", 33263, "Red"]
document.write (steam[3])//Output 33263

Arrays can be easily converted using ToString ()

Copy Code code as follows:

STeam = [Ten, "5565", "pking", 33263, "Red"]
document.write (steam.tostring ())//
Output Results 10,5565,pking,33263,red
document.write (typeof (SS));
Output result string

Use the Join () method if the array is converted to a string and you do not want to use a comma connection.

Copy Code code as follows:

STeam = [Ten, "5565", "pking", 33263, "Red"]
SS = Steam.join ("-");
DD =steam.join ("[")
Output Results 10,5565,pking,33263,red
document.write (ss);
document.write (DD);
Output 10-5565-pking-33263-red 10][5565][pking][33263][red

For strings,JavaScript uses split () to convert to an array

Copy Code code as follows:

var fruit = "Apple,2151,orange";
Sfruit = Fruit.split (",")
document.write (Sfruit); Output Apple,2151,orange
document.write (Sfruit.join ("-")); Output Apple-2151-orange

In the example,JavaScript provides a reverse () method to invert the array.

Copy Code code as follows:

var fruit = "Apple,2151,orange";
Sfruit = Fruit.split (",")
document.write (Sfruit); Output Apple,2151,orange
document.write (Sfruit.join ("-") + "<br>"); Output Apple-2151-orange
document.write (Sfruit.reverse () + "<br>"); output orange,2151,apple
document.write (Sfruit.reverse (). toString () + "<br>"); output Apple,2151,orange

For string inversion, JavaScript has no direct conversion method, we can use split () to convert the string to an array, using Rerverse () to reverse, and then use join to connect, to achieve string inversion.

Copy Code code as follows:

var fruit = "2151,orange,apple";
var sfruit = "iambeijing123";
document.write (Fruit.split (","). Reverse () + "<br>");//2151,orange,apple
document.write (Sfruit.split (""). Reverse (). Join ("") + "<br>");//Output iambeijing123

Sort () enables array elements to be sorted in alphabetical order.

Copy Code code as follows:

Fruit = ["Orange2", "2151", "Orange", "Apple"]
document.write (Fruit.sort ());//Output Results 2151,apple,orange,orange2

About the use of push () and pop ()

Copy Code code as follows:

Sfruit = new Array ();
Sfruit.push ("Red");
Sfruit.push ("green");
Sfruit.push ("Oragen");
Sfruit.push ("Blue");
document.write (Sfruit.length + sfruit.tostring () + "<br>");
var wfruit = Sfruit.pop ();
document.write (wfruit+ "<br>")
document.write (sfruit.tostring ())

As above, JavaScript treats an array as a stack, using push () and pop () for stack and stack processing.

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.