TAILIEUCHUNG - Beginning JavaScript Third Edition phần 3

Vì vậy, những gì các dòng trước đó đang làm là tạo ra một mảng mới bên trong phần tử có chỉ số 0 của mảng nhân viên của chúng tôi. Trong ba dòng tiếp theo, bạn đặt giá trị vào các nhân viên mới được tạo ra [0] array. JavaScript làm cho nó dễ dàng để làm điều này: Bạn chỉ cần nhà nước tên của mảng, x | Chapter 4 JavaScript An Object-Based Language Note that in your for statement you ve used the Array object s length property in the condition statement rather than inserting the length of the array 5 like this for elementindex 0 elementindex 5 elementIndex Why do this After all you know in advance that there are five elements in the array. Well what would happen if you altered the number of elements in our array by adding two more names var names new Array Paul Sarah Louise Adam Bob Karen Steve If you had inserted 5 rather than your loop code wouldn t work as you want it to. It wouldn t display the last two elements unless you changed the condition part of the for loop to 7. By using the length property you ve made life easier because now there is no need to change code elsewhere if you add array elements. Okay you ve put things in ascending order but what if you wanted descending order That is where the reversed method comes in. The reverse Method Putting Your Array into Reverse Order The final method you ll look at for the Array object is the reversed method which no prizes for guessing reverses the order of the array so that the elements at the back are moved to the front. Let s take the shopping list again as an example. Index 0 1 2 II3 II4 Value Eggs Milk Potatoes Cereal Banana If you use the reversed method var myShopping new Array Eggs Milk Potatoes Cereal Banana you end up with the array elements in this order Index 0 1 2 3 4 Value Banana Cereal Potatoes Milk Eggs To prove this you could write it to the page with the join method you saw earlier. var myShoppingList br myShoppingList 135 Chapter 4 JavaScript An Object-Based Language Try It Out Sorting an Array When used in conjunction with the sort method the reverse method can be used to sort an array so that its elements appear in reverse alphabetical or numerical order. This is shown in the following example html body script language JavaScript

TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.