亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

JavaScript Array object

JavaScript Array (array) object

What is an array?

Array objects use separate variable names to store a series of values.

If you have a set of data (for example: car name), there are separate variables as follows:

var car1="Saab";
var car2="Volvo";
var car3="BMW";

However, what if you want to find a certain car? And not 3 cars, but 300 cars? This will not be an easy task!

The best way is to use an array.

Arrays can store all values ??with one variable name, and any value can be accessed with the variable name.

Each element in the array has its own ID so that it can be easily accessed.

Create an array

There are three ways to create an array.

The following code defines an array object named myCars:

1: Conventional method:

var myCars=new Array();
myCars[0 = new Array("Saab","Volvo","BMW");

3: Literal:

var myCars=["Saab","Volvo","BMW"];

Accessing Array

You can access a specific element by specifying the array name and index number.

The following example can access the first value of the myCars array:

var name=myCars[0];

The following example modifies the first element of the array myCars:

myCars[0]="Opel";

[0] is the first element of the array. [1] is the second element of the array.

You can have different objects in an array

All JavaScript variables are objects. Array elements are objects. Functions are objects.

So, you can have different variable types in the array.

You can contain object elements, functions, and arrays in an array:

myArray[0]=Date.now;
myArray[1]=myFunction;
myArray[2]=myCars;

Array methods and properties

Using arrays Object predefined properties and methods:

var x=myCars.length?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????

The role of the array object is to use separate variable names to store a series of values.

<html>
<meta charset="utf-8">
<body>
<script type="text/javascript">
var mycars = new Array()
mycars[0] = "Saab"
mycars[1] = "Volvo"
mycars[2] = "BMW"
for (i=0;i<mycars.length;i++)
{
document.write(mycars[i] + "<br />")
}
</script>
</body>
</html>

Use the for...in statement to loop through the elements in the output array:

<html>
<meta charset="utf-8">
<body>
<script type="text/javascript">
var x
var mycars = new Array()
mycars[0] = "Saab"
mycars[1] = "Volvo"
mycars[2] = "BMW"
for (x in mycars)
{
document.write(mycars[x] + "<br />")
}
</script>
</body>
</html>

How to use the concat() method to merge two arrays:

<html>
<meta charset="utf-8">
<body>
<script type="text/javascript">
var arr = new Array(3)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
var arr2 = new Array(3)
arr2[0] = "James"
arr2[1] = "Adrew"
arr2[2] = "Martin"
document.write(arr.concat(arr2))
</script>
</body>
</html>

How to use The sort() method literally sorts an array:

<html>
<meta charset="utf-8">
<body>
<script type="text/javascript">
var arr = new Array(6)
arr[0] = "George"
arr[1] = "John"
arr[2] = "Thomas"
arr[3] = "James"
arr[4] = "Adrew"
arr[5] = "Martin"
document.write(arr + "<br />")
document.write(arr.sort())
</script>
</body>
</html>

How to use the sort() method to numerically sort an array:

<html>
<meta charset="utf-8">
<body>
<script type="text/javascript">
function sortNumber(a, b)
{
return a - b
}
var arr = new Array(6)
arr[0] = "10"
arr[1] = "5"
arr[2] = "40"
arr[3] = "25"
arr[4] = "1000"
arr[5] = "1"
document.write(arr + "<br />")
document.write(arr.sort(sortNumber))
</script>
</body>
</html>


Continuing Learning
||
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>php中文網(wǎng)(php.cn)</title> </head> <body> <p id="demo">單擊按鈕創(chuàng)建一個數(shù)組,調(diào)用ucase()方法, 并顯示結(jié)果。</p> <button onclick="myFunction()">點我</button> <script> Array.prototype.myUcase=function(){ for (i=0;i<this.length;i++){ this[i]=this[i].toUpperCase(); } } function myFunction(){ var fruits = ["Banana", "Orange", "Apple", "Mango"]; fruits.myUcase(); var x=document.getElementById("demo"); x.innerHTML=fruits; } </script> </body> </html>
submitReset Code