JavaScript Array

JavaScript Array is a good tool for accessing large amounts of data. The Array object is a built-in object of JavaScript and can be called at any time without additional installation. The JavaScript Array object can store other objects or values, which is equivalent to managing large amounts of data. Easy to use, you can use the constructor function (constructor function) to create a new Array object, at the same time or after the completion of the construction, save the Array value to be used, the usage is similar to PHP Array .

Create JavaScript Array

<script type="text/javascript">
 var ArrTest = new Array(); // Declare a new array as ArrTest
 ArrTest[0] = "A"; // The first array element is A
 ArrTest[1] = "B"; // The second array element is B
 ArrTest[2] = "C"; // The third array element is C
</script>

We first declare an array with var ArrTest = new Array(); at the beginning of the example. The name is ArrTest, and then the next three lines are the array value storage. Please note that the number of array elements starts from zero , So when writing the array value, remember to store it from scratch. You can also create a JavaScript Array through the simplified writing method below.

var ArrTest= ["A", "B", "C"];

If you want to change the just created Array, can you? The answer is yes, just need to give the new array value after Array to modify, as shown below:

var ArrTest= ["A", "B", "C"];
ArrTest[1]='BBB';

In this way, the modified new Array will become "ArrTest= ["A", "BBB", "C"]" In this way, since the JavaScript Array value mentioned earlier is arranged from zero, we write in the second line The "ArrTest[1]='BBB'" means to change the second array value to BBB. You can use this technique to adjust other array values. Call JavaScript Array

with JavaScript for loop

<script type="text/javascript">
var ArrTest = new Array();
ArrTest[0] = "A";
ArrTest[1] = "B";
ArrTest[2] = "C";

for(i=0 ;i<3;i++){
 document.write(ArrTest[i]);
}
</script>

When your JavaScript Array is built, of course it is ready to be called! There are many ways to call JavaScript Array, the easiest is to call array elements one by one, but this can only be used in some small places, because the efficiency is not good enough, if you want to call all the array values ​​at once, it is best to use JavaScript Loop . Processing, such as JavaScript for loop or JavaScript while loop, the above example uses JavaScript for loop to call, it will output the result like ABC.

Use JavaScript while loop to call JavaScript Array array

<script
type="text/javascript">
var ArrTest = new Array();
ArrTest[0] =
"A";
ArrTest[1] = "B";
ArrTest[2] = "C";

var i=0;
while(i<3){
 document.write(ArrTest[i]);
 i++;
}
</script>

This example is a demonstration of calling JavaScript Array in JavaScript while loop. The output result is the same as the previous example.

Post a Comment

0 Comments