Home > Web Front-end > JS Tutorial > body text

Javascript basic tutorial array array_basic knowledge

WBOY
Release: 2016-05-16 16:19:40
Original
1099 people have browsed it

Strings, numerical values, and Boolean values ​​all belong to discrete values ​​(scalar). If a variable is discrete, it has only one value at any time.

If you want to use a variable to store a set of values, you need to use an array.

An array is a collection composed of multiple tree values ​​with the same name. Each array in the collection is an element of the array. You can use the variable team to store the name of each member of the team.

In JavaScript, arrays are created using the keyword Array declaration, and the length of the variable can also be declared. For example

Copy code The code is as follows:

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

When the final number of arrays cannot be predicted, the array may be declared without specifying the specific number. For example:

Copy code The code is as follows:

var aTeam = new Array();//When the final number of the array is unknown, you do not need to declare the specific number
aTeam[0] = 1414;
aTeam[1] = "Beijing";
aTeam[2] = 0x4;
aTeam[3] = "i can";
aTeam[4] = "red";
aTeam[5] = "blue";
aTeam[6] = "orange";

In addition, you can create arrays directly

Copy code The code is as follows:

var aTeam = new Array("111","blue","red","beijing");

Like strings, arrays can also use length to get and specify the length of the array.

Copy code The code is as follows:

var aTeam = new Array("111","blue","red","beijing" );
Document.write(aTeam[1] "
");
Document.write(aTeam.length "
")

Note: Can have a deeper understanding of arrays.

Copy code The code is as follows:

var aTeam = new Array("111","blue","red","beijing" );
aTeam[20] = "12415"
Document.write(aTeam[20] "
");
Document.write(aTeam.length "
")

In addition, arrays can also be defined with [ ]. Separate them with commas.

Copy code The code is as follows:

sTeam = [10,"5565","Beijing",33263,"red"]
Document.write(sTeam[3]) //Output 33263

Arrays can be conveniently converted using toString()

Copy code The code is as follows:

sTeam = [10,"5565","pking",33263,"red"]
Document.write(sTeam.toString()) //
//Output result 10,5565,pking,33263,red
document.write(typeof(ss));
//Output result string

If you don’t want to use commas to connect the array to a string, you can use the join() method.

Copy code The code is as follows:

sTeam = [10,"5565","pking",33263,"red"]
ss = sTeam.join("-");
dd =sTeam.join("][")
//Output result 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 it into an array

Copy code The code is 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

Continuing from the above example, javascript provides the reverse() method to reverse the array.

Copy code The code is 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
Document.write(sfruit.reverse() "
");Output orange,2151,apple
document.write(sfruit.reverse().toString() "
");Output apple,2151,orange

For string reversal, JavaScript has no direct conversion method. We can use split() to convert the string into an array, use rerverse() to reverse, and then use join to connect to achieve string reversal.

Copy code The code is as follows:

var fruit = "2151,orange,apple";
var sfruit = "iambeijing123";
document.write(fruit.split(",").reverse() "
");//2151,orange,apple
document.write(sfruit.split("").reverse().join("") "
");//Output iambeijing123

Use sort() to sort the array elements (in alphabetical order).

Copy code The code is as follows:

fruit = ["orange2","2151","orange","apple"]
document.write(fruit.sort());//Output result 2151,apple,orange,orange2

About the usage of push() and pop()

Copy code The code is as follows:

sfruit = new Array();
sfruit.push("red");
sfruit.push("green");
sfruit.push("oragen");
sfruit.push("blue");
document.write(sfruit.length sfruit.toString() "
");
var wfruit = sfruit.pop();
Document.write(wfruit "
")
Document.write(sfruit.toString())

As above, JavaScript treats the array as a stack, and pushes and pops the array through push() and pop().

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template