Strings, numeric values, and boolean values all belong to discrete values (scalars). 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 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 in the team.
In JavaScript, arrays are created using the keyword Array declaration, and colleagues can also declare the length of the variable. For example
The code copy is as follows:
var aTeam = new Array(12);//Declare the length of the variable
When the final number of an array cannot be predicted, the declaration array may not specify a specific number. For example:
The code copy is as follows:
var aTeam = new Array();//If the final number of the array is unknown, you can not 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 an array directly
The code copy 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.
The code copy is as follows:
var aTeam = new Array("111","blue","red","beijing" );
document.write(aTeam[1]+"<br>");
document.write(aTeam.length +"<br>")
Let’s note: A deeper understanding of arrays.
The code copy is as follows:
var aTeam = new Array("111","blue","red","beijing" );
aTeam[20] = "12415"
document.write(aTeam[20]+"<br>");
document.write(aTeam.length +"<br>")
In addition, arrays can also be defined with [ ]. Use commas to separate the middle.
The code copy is as follows:
sTeam = [10,"5565","Beijing",33263,"red"]
document.write(sTeam[3]) //Output 33263
Arrays can be converted easily using toString()
The code copy 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 the array is converted to a string, you do not want to use a comma connection, you can use the join() method.
The code copy 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
The code copy 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
Following the above example, javascript provides reverse() method to invert the array.
The code copy is 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 does not have a direct conversion method. We can use split() to convert the string into an array, use rerverse() to invert, and then use join for connection to achieve string inversion.
The code copy is 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
Use sort() to sort array elements (alphabetical order).
The code copy 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()
The code copy is 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 mentioned above, JavaScript treats arrays as a stack, and push() and pop them through push() and pop().