Giới thiệu. Các đối tượng, trong JavaScript, là kiểu dữ liệu quan trọng nhất của nó và tạo thành các khối xây dựng cho JavaScript hiện đại. Các đối tượng này hoàn toàn khác với các kiểu dữ liệu nguyên thủy của JavaScript [Số, Chuỗi, Boolean, null, không xác định và ký hiệu]. Các đối tượng phức tạp hơn và mỗi đối tượng có thể chứa bất kỳ sự kết hợp nào của các kiểu dữ liệu nguyên thủy này cũng như các kiểu dữ liệu tham chiếu, trong khi mảng là một biến duy nhất được sử dụng để lưu trữ các phần tử khác nhau. Nó thường được sử dụng khi chúng ta muốn lưu trữ một danh sách các phần tử và truy cập chúng bằng một biến duy nhất. Chúng ta có thể chuyển đổi một Đối tượng {} thành một Mảng [] của các cặp khóa-giá trị bằng các phương thức được thảo luận bên dưới.
Phương pháp 1. Trong phương pháp này, chúng ta sẽ sử dụng Object. keys[] và map[] để đạt được điều này.
Tiếp cận. Bằng cách sử dụng đối tượng. keys[], chúng tôi đang trích xuất các khóa từ Đối tượng, sau đó khóa này được chuyển đến hàm map[] ánh xạ khóa và giá trị tương ứng dưới dạng một mảng, như được mô tả trong ví dụ bên dưới.
cú pháp
Object.keys[obj]
Thông số.
- đối tượng. Đó là đối tượng có vô số thuộc tính sẽ được trả về
map[function callback[currentValue[, index[, array]]]{ // Return element for new_array }
Tham số.
- gọi lại. Hàm tạo ra một phần tử của Mảng mới
Ví dụ. Trong ví dụ này, một đối tượng javascript sẽ được chuyển đổi thành một mảng javascript bằng cách sử dụng Object. phương thức keys[] và map[]
jav
map[function callback[currentValue[, index[, array]]]{ // Return element for new_array }8
map[function callback[currentValue[, index[, array]]]{ // Return element for new_array }9
map[function callback[currentValue[, index[, array]]]{ // Return element for new_array }0
map[function callback[currentValue[, index[, array]]]{ // Return element for new_array }9
map[function callback[currentValue[, index[, array]]]{ // Return element for new_array }2
map[function callback[currentValue[, index[, array]]]{ // Return element for new_array }0
map[function callback[currentValue[, index[, array]]]{ // Return element for new_array }1
map[function callback[currentValue[, index[, array]]]{ // Return element for new_array }07
map[function callback[currentValue[, index[, array]]]{ // Return element for new_array }3
map[function callback[currentValue[, index[, array]]]{ // Return element for new_array }09
map[function callback[currentValue[, index[, array]]]{ // Return element for new_array }9
map[function callback[currentValue[, index[, array]]]{ // Return element for new_array }91
map[function callback[currentValue[, index[, array]]]{ // Return element for new_array }7
map[function callback[currentValue[, index[, array]]]{ // Return element for new_array }93
map[function callback[currentValue[, index[, array]]]{ // Return element for new_array }94
map[function callback[currentValue[, index[, array]]]{ // Return element for new_array }95
Mảng kết hợp. Mảng kết hợp được sử dụng để lưu trữ các cặp khóa-giá trị. Ví dụ, để lưu trữ điểm của các môn học khác nhau của một sinh viên trong một mảng, một mảng được lập chỉ mục bằng số sẽ không phải là lựa chọn tốt nhất. Thay vào đó, chúng ta có thể sử dụng tên của các đối tượng tương ứng làm khóa trong mảng kết hợp của mình và giá trị sẽ là số điểm tương ứng mà chúng đạt được. Trong một mảng kết hợp, các cặp khóa-giá trị được liên kết với một ký hiệu.
Phương pháp 1. Trong phương thức này, duyệt qua toàn bộ mảng kết hợp bằng vòng lặp foreach và hiển thị các phần tử chính của mảng.
cú pháp
for [var key in dictionary] { // do something with key }
Ví dụ. Trong ví dụ này, chúng ta sẽ lặp qua mảng kết hợp và in các khóa của mảng.
jav
map[function callback[currentValue[, index[, array]]]{ // Return element for new_array }8
map[function callback[currentValue[, index[, array]]]{ // Return element for new_array }9
map[function callback[currentValue[, index[, array]]]{ // Return element for new_array }1
map[function callback[currentValue[, index[, array]]]{ // Return element for new_array }9
map[function callback[currentValue[, index[, array]]]{ // Return element for new_array }3
map[function callback[currentValue[, index[, array]]]{ // Return element for new_array }9
Keys are listed below Newton Albert Edison Tesla0
Keys are listed below Newton Albert Edison Tesla1
Keys are listed below Newton Albert Edison Tesla2
Keys are listed below Newton Albert Edison Tesla3
Keys are listed below Newton Albert Edison Tesla4
Keys are listed below Newton Albert Edison Tesla5
Keys are listed below Newton Albert Edison Tesla6
Keys are listed below Newton Albert Edison Tesla2
Keys are listed below Newton Albert Edison Tesla8
Keys are listed below Newton Albert Edison Tesla4
Object.keys[obj]0
Keys are listed below Newton Albert Edison Tesla6
Keys are listed below Newton Albert Edison Tesla2
Object.keys[obj]3
Keys are listed below Newton Albert Edison Tesla4
Object.keys[obj]5
Keys are listed below Newton Albert Edison Tesla6
Keys are listed below Newton Albert Edison Tesla2
Object.keys[obj]8
Keys are listed below Newton Albert Edison Tesla4
map[function callback[currentValue[, index[, array]]]{ // Return element for new_array }80
map[function callback[currentValue[, index[, array]]]{ // Return element for new_array }9
map[function callback[currentValue[, index[, array]]]{ // Return element for new_array }82
map[function callback[currentValue[, index[, array]]]{ // Return element for new_array }9
map[function callback[currentValue[, index[, array]]]{ // Return element for new_array }9
map[function callback[currentValue[, index[, array]]]{ // Return element for new_array }85
map[function callback[currentValue[, index[, array]]]{ // Return element for new_array }86
map[function callback[currentValue[, index[, array]]]{ // Return element for new_array }87
map[function callback[currentValue[, index[, array]]]{ // Return element for new_array }9
map[function callback[currentValue[, index[, array]]]{ // Return element for new_array }9
map[function callback[currentValue[, index[, array]]]{ // Return element for new_array }80
map[function callback[currentValue[, index[, array]]]{ // Return element for new_array }9
map[function callback[currentValue[, index[, array]]]{ // Return element for new_array }82
map[function callback[currentValue[, index[, array]]]{ // Return element for new_array }83
Keys are listed below Newton Albert Edison Tesla0
map[function callback[currentValue[, index[, array]]]{ // Return element for new_array }85
map[function callback[currentValue[, index[, array]]]{ // Return element for new_array }86
map[function callback[currentValue[, index[, array]]]{ // Return element for new_array }87
map[function callback[currentValue[, index[, array]]]{ // Return element for new_array }9
map[function callback[currentValue[, index[, array]]]{ // Return element for new_array }89
map[function callback[currentValue[, index[, array]]]{ // Return element for new_array }90
map[function callback[currentValue[, index[, array]]]{ // Return element for new_array }9
Keys are listed below Newton Albert Edison Tesla2
map[function callback[currentValue[, index[, array]]]{ // Return element for new_array }93
Keys are listed below Newton Albert Edison Tesla2
map[function callback[currentValue[, index[, array]]]{ // Return element for new_array }95
map[function callback[currentValue[, index[, array]]]{ // Return element for new_array }9
map[function callback[currentValue[, index[, array]]]{ // Return element for new_array }97
map[function callback[currentValue[, index[, array]]]{ // Return element for new_array }9
map[function callback[currentValue[, index[, array]]]{ // Return element for new_array }97
map[function callback[currentValue[, index[, array]]]{ // Return element for new_array }10
đầu ra
________số 8Phương pháp 2. Sử dụng đối tượng. phím [] chức năng. đối tượng. keys[] là một hàm sẵn có trong javascript có thể được sử dụng để lấy tất cả các khóa của mảng.
cú pháp
Object.keys[obj]
Ví dụ. Chương trình dưới đây minh họa việc sử dụng Object. keys[] để truy cập các khóa của mảng kết hợp.