Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Vì vậy, những gì đang xảy ra? Vâng, sự thật là JavaScript không thực sự hỗ trợ mảng đa chiều, chỉ có những người duy nhất. Tuy nhiên, JavaScript cho phép chúng ta làm giả các mảng đa chiều bằng cách tạo ra một mảng trong mảng khác. | Chapter 2 Data Types and Variables Figure 2-15 How It Works The first thing to do in this script block is declare a variable personnel and tell JavaScript that you want it to be a new array. var personnel new Array Then you do something new you tell JavaScript you want index 0 of the personnel array that is the element personnel 0 to be another new array. personnel 0 new Array So what s going on Well the truth is that JavaScript doesn t actually support multi-dimensional arrays only single ones. However JavaScript enables us to fake multi-dimensional arrays by creating an array inside another array. So what the preceding line is doing is creating a new array inside the element with index 0 of our personnel array. In the next three lines you put values into the newly created personnel 0 array. JavaScript makes it easy to do this You just state the name of the array personnel 0 followed by another index in square brackets. The first index 0 belongs to the personnel array the second index belongs to the personnel 0 array. personnel 0 0 Name0 personnel 0 1 Age0 personnel 0 2 Address0 56 Chapter 2 Data Types and Variables After these lines of code our array looks like this Index 0 0 Name0 1 Age0 2 Address0 The numbers at the top at the moment just 0 refer to the personnel array. The numbers going down the side 0 1 and 2 are actually indices for the new personnel 0 array inside the personnel array. For the second person s details you repeat the process but this time you are using the personnel array element with index 1. personnel 1 new Array personnel 1 0 personnel 1 1 personnel 1 2 Name1 Age1 Address1 Now your array looks like this 0 1 0 Name0 Name1 1 Age0 Age1 2 Address0 Address1 You create a third person s details in the next few lines. You are now using the element with index 2 inside the personnel array to create a new array. personnel 2 new Array personnel 2 0 personnel 2 1 personnel 2 2 Name2 Age2 Address2 The array now looks like this Index 0 1 2 0 Name0 Name1 .