Các đối tượng Bản đồ có trong JavaScript không?

Bản đồ là tập hợp các mục dữ liệu có khóa, giống như một

let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
5. Nhưng sự khác biệt chính là
let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
3 cho phép các loại khóa

Các phương thức và thuộc tính là

  • let john = { name: "John" };
    
    // for every user, let's store their visits count
    let visitsCountMap = new Map[];
    
    // john is the key for the map
    visitsCountMap.set[john, 123];
    
    alert[ visitsCountMap.get[john] ]; // 123
    7 – tạo bản đồ
  • let john = { name: "John" };
    
    // for every user, let's store their visits count
    let visitsCountMap = new Map[];
    
    // john is the key for the map
    visitsCountMap.set[john, 123];
    
    alert[ visitsCountMap.get[john] ]; // 123
    8 – lưu trữ giá trị bằng khóa
  • let john = { name: "John" };
    
    // for every user, let's store their visits count
    let visitsCountMap = new Map[];
    
    // john is the key for the map
    visitsCountMap.set[john, 123];
    
    alert[ visitsCountMap.get[john] ]; // 123
    9 – trả về giá trị theo khóa,
    let john = { name: "John" };
    let ben = { name: "Ben" };
    
    let visitsCountObj = {}; // try to use an object
    
    visitsCountObj[ben] = 234; // try to use ben object as the key
    visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced
    
    // That's what got written!
    alert[ visitsCountObj["[object Object]"] ]; // 123
    0 nếu
    let john = { name: "John" };
    let ben = { name: "Ben" };
    
    let visitsCountObj = {}; // try to use an object
    
    visitsCountObj[ben] = 234; // try to use ben object as the key
    visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced
    
    // That's what got written!
    alert[ visitsCountObj["[object Object]"] ]; // 123
    1 không tồn tại trên bản đồ
  • let john = { name: "John" };
    let ben = { name: "Ben" };
    
    let visitsCountObj = {}; // try to use an object
    
    visitsCountObj[ben] = 234; // try to use ben object as the key
    visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced
    
    // That's what got written!
    alert[ visitsCountObj["[object Object]"] ]; // 123
    2 – trả về
    let john = { name: "John" };
    let ben = { name: "Ben" };
    
    let visitsCountObj = {}; // try to use an object
    
    visitsCountObj[ben] = 234; // try to use ben object as the key
    visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced
    
    // That's what got written!
    alert[ visitsCountObj["[object Object]"] ]; // 123
    3 nếu tồn tại
    let john = { name: "John" };
    let ben = { name: "Ben" };
    
    let visitsCountObj = {}; // try to use an object
    
    visitsCountObj[ben] = 234; // try to use ben object as the key
    visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced
    
    // That's what got written!
    alert[ visitsCountObj["[object Object]"] ]; // 123
    1, ngược lại là
    let john = { name: "John" };
    let ben = { name: "Ben" };
    
    let visitsCountObj = {}; // try to use an object
    
    visitsCountObj[ben] = 234; // try to use ben object as the key
    visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced
    
    // That's what got written!
    alert[ visitsCountObj["[object Object]"] ]; // 123
    5
  • let john = { name: "John" };
    let ben = { name: "Ben" };
    
    let visitsCountObj = {}; // try to use an object
    
    visitsCountObj[ben] = 234; // try to use ben object as the key
    visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced
    
    // That's what got written!
    alert[ visitsCountObj["[object Object]"] ]; // 123
    6 – xóa phần tử [cặp khóa/giá trị] bằng khóa
  • let john = { name: "John" };
    let ben = { name: "Ben" };
    
    let visitsCountObj = {}; // try to use an object
    
    visitsCountObj[ben] = 234; // try to use ben object as the key
    visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced
    
    // That's what got written!
    alert[ visitsCountObj["[object Object]"] ]; // 123
    7 – xóa mọi thứ khỏi bản đồ
  • let john = { name: "John" };
    let ben = { name: "Ben" };
    
    let visitsCountObj = {}; // try to use an object
    
    visitsCountObj[ben] = 234; // try to use ben object as the key
    visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced
    
    // That's what got written!
    alert[ visitsCountObj["[object Object]"] ]; // 123
    8 – trả về số lượng phần tử hiện tại

Ví dụ

let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
4

Như chúng ta có thể thấy, không giống như các đối tượng, các khóa không được chuyển thành chuỗi. Bất kỳ loại chìa khóa là có thể

let john = { name: "John" };
let ben = { name: "Ben" };

let visitsCountObj = {}; // try to use an object

visitsCountObj[ben] = 234; // try to use ben object as the key
visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced

// That's what got written!
alert[ visitsCountObj["[object Object]"] ]; // 123
9 không phải là cách đúng đắn để sử dụng
let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
3

Mặc dù

let john = { name: "John" };
let ben = { name: "Ben" };

let visitsCountObj = {}; // try to use an object

visitsCountObj[ben] = 234; // try to use ben object as the key
visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced

// That's what got written!
alert[ visitsCountObj["[object Object]"] ]; // 123
9 cũng hoạt động, e. g. chúng ta có thể đặt
let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
82, điều này đang coi
let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
83 là một đối tượng JavaScript đơn giản, do đó, nó bao hàm tất cả các giới hạn tương ứng [chỉ các khóa chuỗi/ký hiệu, v.v.]

Vì vậy, chúng ta nên sử dụng phương pháp

let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
83.
let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
85,
let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
86, v.v.

Bản đồ cũng có thể sử dụng các đối tượng làm chìa khóa

Ví dụ

let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123

Sử dụng các đối tượng làm chìa khóa là một trong những tính năng quan trọng và đáng chú ý nhất của

let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
3. Điều tương tự không được tính cho
let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
5. Chuỗi làm khóa trong
let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
5 cũng được, nhưng chúng tôi không thể sử dụng một
let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
5 khác làm khóa trong
let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
5

Hãy thử

let john = { name: "John" };
let ben = { name: "Ben" };

let visitsCountObj = {}; // try to use an object

visitsCountObj[ben] = 234; // try to use ben object as the key
visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced

// That's what got written!
alert[ visitsCountObj["[object Object]"] ]; // 123

let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
02 là một đối tượng, nó chuyển đổi tất cả các khóa
let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
5, chẳng hạn như
let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
04 và
let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
05 ở trên, thành cùng một chuỗi
let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
06. Chắc chắn không phải những gì chúng ta muốn

Cách

let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
3 so sánh các khóa

Để kiểm tra tính tương đương của các khóa,

let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
3 sử dụng thuật toán SameValueZero. Nó gần giống như đẳng thức nghiêm ngặt
let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
09, nhưng điểm khác biệt là
let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
80 được coi là bằng với
let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
80. Vì vậy,
let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
80 cũng có thể được sử dụng làm khóa

Thuật toán này không thể thay đổi hoặc tùy chỉnh

xiềng xích

Mỗi cuộc gọi

let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
83 đều trả về bản đồ, vì vậy chúng tôi có thể "xâu chuỗi" các cuộc gọi

let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
8

Lặp lại trên Bản đồ

Để lặp qua một

let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
83, có 3 phương pháp

  • let john = { name: "John" };
    
    // for every user, let's store their visits count
    let visitsCountMap = new Map[];
    
    // john is the key for the map
    visitsCountMap.set[john, 123];
    
    alert[ visitsCountMap.get[john] ]; // 123
    85 – trả về một lần lặp cho các khóa,
  • let john = { name: "John" };
    
    // for every user, let's store their visits count
    let visitsCountMap = new Map[];
    
    // john is the key for the map
    visitsCountMap.set[john, 123];
    
    alert[ visitsCountMap.get[john] ]; // 123
    86 – trả về một giá trị có thể lặp lại,
  • let john = { name: "John" };
    
    // for every user, let's store their visits count
    let visitsCountMap = new Map[];
    
    // john is the key for the map
    visitsCountMap.set[john, 123];
    
    alert[ visitsCountMap.get[john] ]; // 123
    87 – trả về một lần lặp cho các mục nhập
    let john = { name: "John" };
    
    // for every user, let's store their visits count
    let visitsCountMap = new Map[];
    
    // john is the key for the map
    visitsCountMap.set[john, 123];
    
    alert[ visitsCountMap.get[john] ]; // 123
    88, nó được sử dụng theo mặc định trong
    let john = { name: "John" };
    
    // for every user, let's store their visits count
    let visitsCountMap = new Map[];
    
    // john is the key for the map
    visitsCountMap.set[john, 123];
    
    alert[ visitsCountMap.get[john] ]; // 123
    89

Ví dụ

let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
0

Thứ tự chèn được sử dụng

Việc lặp đi lặp lại theo thứ tự như các giá trị đã được chèn vào.

let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
3 duy trì thứ tự này, không giống như một
let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
5 thông thường

Bên cạnh đó,

let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
3 có phương thức
let john = { name: "John" };
let ben = { name: "Ben" };

let visitsCountObj = {}; // try to use an object

visitsCountObj[ben] = 234; // try to use ben object as the key
visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced

// That's what got written!
alert[ visitsCountObj["[object Object]"] ]; // 123
33 tích hợp, tương tự như
let john = { name: "John" };
let ben = { name: "Ben" };

let visitsCountObj = {}; // try to use an object

visitsCountObj[ben] = 234; // try to use ben object as the key
visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced

// That's what got written!
alert[ visitsCountObj["[object Object]"] ]; // 123
34

let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
8

Mục tiêu. mục. Bản đồ từ đối tượng

Khi một

let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
3 được tạo, chúng ta có thể truyền một mảng [hoặc một mảng khác có thể lặp lại] với các cặp khóa/giá trị để khởi tạo, như thế này

let john = { name: "John" };
let ben = { name: "Ben" };

let visitsCountObj = {}; // try to use an object

visitsCountObj[ben] = 234; // try to use ben object as the key
visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced

// That's what got written!
alert[ visitsCountObj["[object Object]"] ]; // 123
3

Nếu chúng ta có một đối tượng đơn giản và chúng ta muốn tạo một

let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
3 từ nó, thì chúng ta có thể sử dụng phương thức tích hợp sẵn Object. các mục [obj] trả về một mảng các cặp khóa/giá trị cho một đối tượng chính xác ở định dạng đó

Vì vậy, chúng ta có thể tạo bản đồ từ một đối tượng như thế này

let john = { name: "John" };
let ben = { name: "Ben" };

let visitsCountObj = {}; // try to use an object

visitsCountObj[ben] = 234; // try to use ben object as the key
visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced

// That's what got written!
alert[ visitsCountObj["[object Object]"] ]; // 123
9

Ở đây,

let john = { name: "John" };
let ben = { name: "Ben" };

let visitsCountObj = {}; // try to use an object

visitsCountObj[ben] = 234; // try to use ben object as the key
visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced

// That's what got written!
alert[ visitsCountObj["[object Object]"] ]; // 123
37 trả về mảng các cặp khóa/giá trị.
let john = { name: "John" };
let ben = { name: "Ben" };

let visitsCountObj = {}; // try to use an object

visitsCountObj[ben] = 234; // try to use ben object as the key
visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced

// That's what got written!
alert[ visitsCountObj["[object Object]"] ]; // 123
38. Đó là những gì
let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
3 cần

Mục tiêu. fromEntries. Đối tượng từ Bản đồ

Chúng ta vừa xem cách tạo

let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
3 từ một đối tượng đơn giản với
let john = { name: "John" };
let ben = { name: "Ben" };

let visitsCountObj = {}; // try to use an object

visitsCountObj[ben] = 234; // try to use ben object as the key
visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced

// That's what got written!
alert[ visitsCountObj["[object Object]"] ]; // 123
91

Có phương thức

let john = { name: "John" };
let ben = { name: "Ben" };

let visitsCountObj = {}; // try to use an object

visitsCountObj[ben] = 234; // try to use ben object as the key
visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced

// That's what got written!
alert[ visitsCountObj["[object Object]"] ]; // 123
92 thực hiện ngược lại. đưa ra một mảng gồm các cặp
let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
88, nó tạo ra một đối tượng từ chúng

let john = { name: "John" };
let ben = { name: "Ben" };

let visitsCountObj = {}; // try to use an object

visitsCountObj[ben] = 234; // try to use ben object as the key
visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced

// That's what got written!
alert[ visitsCountObj["[object Object]"] ]; // 123
7

Chúng ta có thể sử dụng

let john = { name: "John" };
let ben = { name: "Ben" };

let visitsCountObj = {}; // try to use an object

visitsCountObj[ben] = 234; // try to use ben object as the key
visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced

// That's what got written!
alert[ visitsCountObj["[object Object]"] ]; // 123
92 để lấy một đối tượng đơn giản từ
let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
3

E. g. chúng tôi lưu trữ dữ liệu trong một

let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
3, nhưng chúng tôi cần chuyển dữ liệu đó tới mã của bên thứ 3 mong đợi một đối tượng đơn giản

chúng ta đi đây

let john = { name: "John" };
let ben = { name: "Ben" };

let visitsCountObj = {}; // try to use an object

visitsCountObj[ben] = 234; // try to use ben object as the key
visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced

// That's what got written!
alert[ visitsCountObj["[object Object]"] ]; // 123
1

Một cuộc gọi đến

let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
87 trả về một cặp khóa/giá trị có thể lặp lại, chính xác ở định dạng phù hợp cho
let john = { name: "John" };
let ben = { name: "Ben" };

let visitsCountObj = {}; // try to use an object

visitsCountObj[ben] = 234; // try to use ben object as the key
visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced

// That's what got written!
alert[ visitsCountObj["[object Object]"] ]; // 123
92

Chúng tôi cũng có thể làm cho dòng

let john = { name: "John" };
let ben = { name: "Ben" };

let visitsCountObj = {}; // try to use an object

visitsCountObj[ben] = 234; // try to use ben object as the key
visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced

// That's what got written!
alert[ visitsCountObj["[object Object]"] ]; // 123
99 ngắn hơn

let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
0

Điều đó cũng tương tự, bởi vì

let john = { name: "John" };
let ben = { name: "Ben" };

let visitsCountObj = {}; // try to use an object

visitsCountObj[ben] = 234; // try to use ben object as the key
visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced

// That's what got written!
alert[ visitsCountObj["[object Object]"] ]; // 123
92 mong đợi một đối tượng có thể lặp lại làm đối số. Không nhất thiết phải là một mảng. Và phép lặp tiêu chuẩn cho
let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
83 trả về các cặp khóa/giá trị giống như
let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
87. Vì vậy, chúng tôi nhận được một đối tượng đơn giản có cùng khóa/giá trị như
let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
83

Bố trí

Một

let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
4 là một tập hợp loại đặc biệt – “tập hợp các giá trị” [không có khóa], trong đó mỗi giá trị chỉ có thể xuất hiện một lần

phương pháp chính của nó là

  • let john = { name: "John" };
    let ben = { name: "Ben" };
    
    let visitsCountObj = {}; // try to use an object
    
    visitsCountObj[ben] = 234; // try to use ben object as the key
    visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced
    
    // That's what got written!
    alert[ visitsCountObj["[object Object]"] ]; // 123
    75 – tạo tập hợp và nếu một đối tượng
    let john = { name: "John" };
    let ben = { name: "Ben" };
    
    let visitsCountObj = {}; // try to use an object
    
    visitsCountObj[ben] = 234; // try to use ben object as the key
    visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced
    
    // That's what got written!
    alert[ visitsCountObj["[object Object]"] ]; // 123
    76 được cung cấp [thường là một mảng], hãy sao chép các giá trị từ đối tượng đó vào tập hợp
  • let john = { name: "John" };
    let ben = { name: "Ben" };
    
    let visitsCountObj = {}; // try to use an object
    
    visitsCountObj[ben] = 234; // try to use ben object as the key
    visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced
    
    // That's what got written!
    alert[ visitsCountObj["[object Object]"] ]; // 123
    77 – thêm một giá trị, trả về chính tập hợp đó
  • let john = { name: "John" };
    let ben = { name: "Ben" };
    
    let visitsCountObj = {}; // try to use an object
    
    visitsCountObj[ben] = 234; // try to use ben object as the key
    visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced
    
    // That's what got written!
    alert[ visitsCountObj["[object Object]"] ]; // 123
    78 – loại bỏ giá trị, trả về
    let john = { name: "John" };
    let ben = { name: "Ben" };
    
    let visitsCountObj = {}; // try to use an object
    
    visitsCountObj[ben] = 234; // try to use ben object as the key
    visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced
    
    // That's what got written!
    alert[ visitsCountObj["[object Object]"] ]; // 123
    3 nếu
    let john = { name: "John" };
    let ben = { name: "Ben" };
    
    let visitsCountObj = {}; // try to use an object
    
    visitsCountObj[ben] = 234; // try to use ben object as the key
    visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced
    
    // That's what got written!
    alert[ visitsCountObj["[object Object]"] ]; // 123
    10 tồn tại tại thời điểm gọi, nếu không thì
    let john = { name: "John" };
    let ben = { name: "Ben" };
    
    let visitsCountObj = {}; // try to use an object
    
    visitsCountObj[ben] = 234; // try to use ben object as the key
    visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced
    
    // That's what got written!
    alert[ visitsCountObj["[object Object]"] ]; // 123
    5
  • let john = { name: "John" };
    let ben = { name: "Ben" };
    
    let visitsCountObj = {}; // try to use an object
    
    visitsCountObj[ben] = 234; // try to use ben object as the key
    visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced
    
    // That's what got written!
    alert[ visitsCountObj["[object Object]"] ]; // 123
    12 – trả về
    let john = { name: "John" };
    let ben = { name: "Ben" };
    
    let visitsCountObj = {}; // try to use an object
    
    visitsCountObj[ben] = 234; // try to use ben object as the key
    visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced
    
    // That's what got written!
    alert[ visitsCountObj["[object Object]"] ]; // 123
    3 nếu giá trị tồn tại trong tập hợp, nếu không thì
    let john = { name: "John" };
    let ben = { name: "Ben" };
    
    let visitsCountObj = {}; // try to use an object
    
    visitsCountObj[ben] = 234; // try to use ben object as the key
    visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced
    
    // That's what got written!
    alert[ visitsCountObj["[object Object]"] ]; // 123
    5
  • let john = { name: "John" };
    let ben = { name: "Ben" };
    
    let visitsCountObj = {}; // try to use an object
    
    visitsCountObj[ben] = 234; // try to use ben object as the key
    visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced
    
    // That's what got written!
    alert[ visitsCountObj["[object Object]"] ]; // 123
    15 – loại bỏ mọi thứ khỏi bộ
  • let john = { name: "John" };
    let ben = { name: "Ben" };
    
    let visitsCountObj = {}; // try to use an object
    
    visitsCountObj[ben] = 234; // try to use ben object as the key
    visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced
    
    // That's what got written!
    alert[ visitsCountObj["[object Object]"] ]; // 123
    16 – là số nguyên tố

Tính năng chính là các cuộc gọi lặp đi lặp lại của

let john = { name: "John" };
let ben = { name: "Ben" };

let visitsCountObj = {}; // try to use an object

visitsCountObj[ben] = 234; // try to use ben object as the key
visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced

// That's what got written!
alert[ visitsCountObj["[object Object]"] ]; // 123
77 với cùng một giá trị không làm gì cả. Đó là lý do tại sao mỗi giá trị chỉ xuất hiện trong một
let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
4 một lần

Ví dụ: chúng tôi có khách đến và chúng tôi muốn ghi nhớ tất cả mọi người. Nhưng các lượt truy cập lặp đi lặp lại sẽ không dẫn đến trùng lặp. Một khách truy cập phải được "đếm" một lần duy nhất

let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
4 chỉ là điều phù hợp cho điều đó

let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
1

Giải pháp thay thế cho

let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
4 có thể là một mảng người dùng và mã để kiểm tra các bản sao trên mỗi lần chèn bằng cách sử dụng mảng. tìm thấy. Nhưng hiệu suất sẽ tệ hơn nhiều, bởi vì phương pháp này đi qua toàn bộ mảng kiểm tra mọi phần tử.
let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
4 được tối ưu hóa nội bộ tốt hơn nhiều để kiểm tra tính duy nhất

Lặp lại Set

Chúng ta có thể lặp qua một tập hợp bằng

let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
89 hoặc bằng cách sử dụng
let john = { name: "John" };
let ben = { name: "Ben" };

let visitsCountObj = {}; // try to use an object

visitsCountObj[ben] = 234; // try to use ben object as the key
visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced

// That's what got written!
alert[ visitsCountObj["[object Object]"] ]; // 123
33

let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
2

Lưu ý điều buồn cười. Hàm gọi lại được truyền trong

let john = { name: "John" };
let ben = { name: "Ben" };

let visitsCountObj = {}; // try to use an object

visitsCountObj[ben] = 234; // try to use ben object as the key
visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced

// That's what got written!
alert[ visitsCountObj["[object Object]"] ]; // 123
33 có 3 đối số. một
let john = { name: "John" };
let ben = { name: "Ben" };

let visitsCountObj = {}; // try to use an object

visitsCountObj[ben] = 234; // try to use ben object as the key
visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced

// That's what got written!
alert[ visitsCountObj["[object Object]"] ]; // 123
10, sau đó là cùng một giá trị
let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
06 và sau đó là đối tượng đích. Thật vậy, cùng một giá trị xuất hiện trong các đối số hai lần

Đó là khả năng tương thích với

let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
3 khi cuộc gọi lại được thông qua
let john = { name: "John" };
let ben = { name: "Ben" };

let visitsCountObj = {}; // try to use an object

visitsCountObj[ben] = 234; // try to use ben object as the key
visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced

// That's what got written!
alert[ visitsCountObj["[object Object]"] ]; // 123
33 có ba đối số. Trông hơi lạ, chắc chắn. Nhưng điều này có thể giúp thay thế
let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
3 bằng
let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
4 trong một số trường hợp một cách dễ dàng và ngược lại

Các phương pháp tương tự mà

let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
3 dành cho các trình vòng lặp cũng được hỗ trợ

  • let john = { name: "John" };
    
    // for every user, let's store their visits count
    let visitsCountMap = new Map[];
    
    // john is the key for the map
    visitsCountMap.set[john, 123];
    
    alert[ visitsCountMap.get[john] ]; // 123
    12 – trả về một đối tượng có thể lặp lại cho các giá trị,
  • let john = { name: "John" };
    
    // for every user, let's store their visits count
    let visitsCountMap = new Map[];
    
    // john is the key for the map
    visitsCountMap.set[john, 123];
    
    alert[ visitsCountMap.get[john] ]; // 123
    13 – giống như
    let john = { name: "John" };
    
    // for every user, let's store their visits count
    let visitsCountMap = new Map[];
    
    // john is the key for the map
    visitsCountMap.set[john, 123];
    
    alert[ visitsCountMap.get[john] ]; // 123
    12, để tương thích với
    let john = { name: "John" };
    
    // for every user, let's store their visits count
    let visitsCountMap = new Map[];
    
    // john is the key for the map
    visitsCountMap.set[john, 123];
    
    alert[ visitsCountMap.get[john] ]; // 123
    3,
  • let john = { name: "John" };
    
    // for every user, let's store their visits count
    let visitsCountMap = new Map[];
    
    // john is the key for the map
    visitsCountMap.set[john, 123];
    
    alert[ visitsCountMap.get[john] ]; // 123
    16 – trả về một đối tượng có thể lặp lại cho các mục nhập
    let john = { name: "John" };
    
    // for every user, let's store their visits count
    let visitsCountMap = new Map[];
    
    // john is the key for the map
    visitsCountMap.set[john, 123];
    
    alert[ visitsCountMap.get[john] ]; // 123
    17, tồn tại để tương thích với
    let john = { name: "John" };
    
    // for every user, let's store their visits count
    let visitsCountMap = new Map[];
    
    // john is the key for the map
    visitsCountMap.set[john, 123];
    
    alert[ visitsCountMap.get[john] ]; // 123
    3

Tóm lược

let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
3 – là tập hợp các giá trị được khóa

Phương thức và thuộc tính

  • let john = { name: "John" };
    
    // for every user, let's store their visits count
    let visitsCountMap = new Map[];
    
    // john is the key for the map
    visitsCountMap.set[john, 123];
    
    alert[ visitsCountMap.get[john] ]; // 123
    20 – tạo bản đồ, với tùy chọn
    let john = { name: "John" };
    let ben = { name: "Ben" };
    
    let visitsCountObj = {}; // try to use an object
    
    visitsCountObj[ben] = 234; // try to use ben object as the key
    visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced
    
    // That's what got written!
    alert[ visitsCountObj["[object Object]"] ]; // 123
    76 [e. g. mảng] của
    let john = { name: "John" };
    
    // for every user, let's store their visits count
    let visitsCountMap = new Map[];
    
    // john is the key for the map
    visitsCountMap.set[john, 123];
    
    alert[ visitsCountMap.get[john] ]; // 123
    22 cặp để khởi tạo
  • let john = { name: "John" };
    
    // for every user, let's store their visits count
    let visitsCountMap = new Map[];
    
    // john is the key for the map
    visitsCountMap.set[john, 123];
    
    alert[ visitsCountMap.get[john] ]; // 123
    8 – lưu trữ giá trị theo khóa, trả về chính bản đồ
  • let john = { name: "John" };
    
    // for every user, let's store their visits count
    let visitsCountMap = new Map[];
    
    // john is the key for the map
    visitsCountMap.set[john, 123];
    
    alert[ visitsCountMap.get[john] ]; // 123
    9 – trả về giá trị theo khóa,
    let john = { name: "John" };
    let ben = { name: "Ben" };
    
    let visitsCountObj = {}; // try to use an object
    
    visitsCountObj[ben] = 234; // try to use ben object as the key
    visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced
    
    // That's what got written!
    alert[ visitsCountObj["[object Object]"] ]; // 123
    0 nếu
    let john = { name: "John" };
    let ben = { name: "Ben" };
    
    let visitsCountObj = {}; // try to use an object
    
    visitsCountObj[ben] = 234; // try to use ben object as the key
    visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced
    
    // That's what got written!
    alert[ visitsCountObj["[object Object]"] ]; // 123
    1 không tồn tại trên bản đồ
  • let john = { name: "John" };
    let ben = { name: "Ben" };
    
    let visitsCountObj = {}; // try to use an object
    
    visitsCountObj[ben] = 234; // try to use ben object as the key
    visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced
    
    // That's what got written!
    alert[ visitsCountObj["[object Object]"] ]; // 123
    2 – trả về
    let john = { name: "John" };
    let ben = { name: "Ben" };
    
    let visitsCountObj = {}; // try to use an object
    
    visitsCountObj[ben] = 234; // try to use ben object as the key
    visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced
    
    // That's what got written!
    alert[ visitsCountObj["[object Object]"] ]; // 123
    3 nếu tồn tại
    let john = { name: "John" };
    let ben = { name: "Ben" };
    
    let visitsCountObj = {}; // try to use an object
    
    visitsCountObj[ben] = 234; // try to use ben object as the key
    visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced
    
    // That's what got written!
    alert[ visitsCountObj["[object Object]"] ]; // 123
    1, ngược lại là
    let john = { name: "John" };
    let ben = { name: "Ben" };
    
    let visitsCountObj = {}; // try to use an object
    
    visitsCountObj[ben] = 234; // try to use ben object as the key
    visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced
    
    // That's what got written!
    alert[ visitsCountObj["[object Object]"] ]; // 123
    5
  • let john = { name: "John" };
    let ben = { name: "Ben" };
    
    let visitsCountObj = {}; // try to use an object
    
    visitsCountObj[ben] = 234; // try to use ben object as the key
    visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced
    
    // That's what got written!
    alert[ visitsCountObj["[object Object]"] ]; // 123
    6 – loại bỏ phần tử bằng phím, trả về
    let john = { name: "John" };
    let ben = { name: "Ben" };
    
    let visitsCountObj = {}; // try to use an object
    
    visitsCountObj[ben] = 234; // try to use ben object as the key
    visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced
    
    // That's what got written!
    alert[ visitsCountObj["[object Object]"] ]; // 123
    3 nếu
    let john = { name: "John" };
    let ben = { name: "Ben" };
    
    let visitsCountObj = {}; // try to use an object
    
    visitsCountObj[ben] = 234; // try to use ben object as the key
    visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced
    
    // That's what got written!
    alert[ visitsCountObj["[object Object]"] ]; // 123
    1 tồn tại tại thời điểm gọi, nếu không thì
    let john = { name: "John" };
    let ben = { name: "Ben" };
    
    let visitsCountObj = {}; // try to use an object
    
    visitsCountObj[ben] = 234; // try to use ben object as the key
    visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced
    
    // That's what got written!
    alert[ visitsCountObj["[object Object]"] ]; // 123
    5
  • let john = { name: "John" };
    let ben = { name: "Ben" };
    
    let visitsCountObj = {}; // try to use an object
    
    visitsCountObj[ben] = 234; // try to use ben object as the key
    visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced
    
    // That's what got written!
    alert[ visitsCountObj["[object Object]"] ]; // 123
    7 – xóa mọi thứ khỏi bản đồ
  • let john = { name: "John" };
    let ben = { name: "Ben" };
    
    let visitsCountObj = {}; // try to use an object
    
    visitsCountObj[ben] = 234; // try to use ben object as the key
    visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced
    
    // That's what got written!
    alert[ visitsCountObj["[object Object]"] ]; // 123
    8 – trả về số lượng phần tử hiện tại

Sự khác biệt so với một

let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
5 thông thường

  • Bất kỳ chìa khóa, đồ vật nào cũng có thể là chìa khóa
  • Các phương pháp thuận tiện bổ sung, thuộc tính
    let john = { name: "John" };
    
    // for every user, let's store their visits count
    let visitsCountMap = new Map[];
    
    // john is the key for the map
    visitsCountMap.set[john, 123];
    
    alert[ visitsCountMap.get[john] ]; // 123
    38

let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
4 – là một tập hợp các giá trị duy nhất

Phương thức và thuộc tính

  • let john = { name: "John" };
    let ben = { name: "Ben" };
    
    let visitsCountObj = {}; // try to use an object
    
    visitsCountObj[ben] = 234; // try to use ben object as the key
    visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced
    
    // That's what got written!
    alert[ visitsCountObj["[object Object]"] ]; // 123
    75 – tạo tập hợp, với tùy chọn
    let john = { name: "John" };
    let ben = { name: "Ben" };
    
    let visitsCountObj = {}; // try to use an object
    
    visitsCountObj[ben] = 234; // try to use ben object as the key
    visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced
    
    // That's what got written!
    alert[ visitsCountObj["[object Object]"] ]; // 123
    76 [e. g. mảng] của các giá trị để khởi tạo
  • let john = { name: "John" };
    let ben = { name: "Ben" };
    
    let visitsCountObj = {}; // try to use an object
    
    visitsCountObj[ben] = 234; // try to use ben object as the key
    visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced
    
    // That's what got written!
    alert[ visitsCountObj["[object Object]"] ]; // 123
    77 – thêm một giá trị [không làm gì nếu
    let john = { name: "John" };
    let ben = { name: "Ben" };
    
    let visitsCountObj = {}; // try to use an object
    
    visitsCountObj[ben] = 234; // try to use ben object as the key
    visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced
    
    // That's what got written!
    alert[ visitsCountObj["[object Object]"] ]; // 123
    10 tồn tại], trả về chính tập hợp đó
  • let john = { name: "John" };
    let ben = { name: "Ben" };
    
    let visitsCountObj = {}; // try to use an object
    
    visitsCountObj[ben] = 234; // try to use ben object as the key
    visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced
    
    // That's what got written!
    alert[ visitsCountObj["[object Object]"] ]; // 123
    78 – loại bỏ giá trị, trả về
    let john = { name: "John" };
    let ben = { name: "Ben" };
    
    let visitsCountObj = {}; // try to use an object
    
    visitsCountObj[ben] = 234; // try to use ben object as the key
    visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced
    
    // That's what got written!
    alert[ visitsCountObj["[object Object]"] ]; // 123
    3 nếu
    let john = { name: "John" };
    let ben = { name: "Ben" };
    
    let visitsCountObj = {}; // try to use an object
    
    visitsCountObj[ben] = 234; // try to use ben object as the key
    visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced
    
    // That's what got written!
    alert[ visitsCountObj["[object Object]"] ]; // 123
    10 tồn tại tại thời điểm gọi, nếu không thì
    let john = { name: "John" };
    let ben = { name: "Ben" };
    
    let visitsCountObj = {}; // try to use an object
    
    visitsCountObj[ben] = 234; // try to use ben object as the key
    visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced
    
    // That's what got written!
    alert[ visitsCountObj["[object Object]"] ]; // 123
    5
  • let john = { name: "John" };
    let ben = { name: "Ben" };
    
    let visitsCountObj = {}; // try to use an object
    
    visitsCountObj[ben] = 234; // try to use ben object as the key
    visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced
    
    // That's what got written!
    alert[ visitsCountObj["[object Object]"] ]; // 123
    12 – trả về
    let john = { name: "John" };
    let ben = { name: "Ben" };
    
    let visitsCountObj = {}; // try to use an object
    
    visitsCountObj[ben] = 234; // try to use ben object as the key
    visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced
    
    // That's what got written!
    alert[ visitsCountObj["[object Object]"] ]; // 123
    3 nếu giá trị tồn tại trong tập hợp, nếu không thì
    let john = { name: "John" };
    let ben = { name: "Ben" };
    
    let visitsCountObj = {}; // try to use an object
    
    visitsCountObj[ben] = 234; // try to use ben object as the key
    visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced
    
    // That's what got written!
    alert[ visitsCountObj["[object Object]"] ]; // 123
    5
  • let john = { name: "John" };
    let ben = { name: "Ben" };
    
    let visitsCountObj = {}; // try to use an object
    
    visitsCountObj[ben] = 234; // try to use ben object as the key
    visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced
    
    // That's what got written!
    alert[ visitsCountObj["[object Object]"] ]; // 123
    15 – loại bỏ mọi thứ khỏi bộ
  • let john = { name: "John" };
    let ben = { name: "Ben" };
    
    let visitsCountObj = {}; // try to use an object
    
    visitsCountObj[ben] = 234; // try to use ben object as the key
    visitsCountObj[john] = 123; // try to use john object as the key, ben object will get replaced
    
    // That's what got written!
    alert[ visitsCountObj["[object Object]"] ]; // 123
    16 – là số nguyên tố

Việc lặp lại trên

let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
3 và
let john = { name: "John" };

// for every user, let's store their visits count
let visitsCountMap = new Map[];

// john is the key for the map
visitsCountMap.set[john, 123];

alert[ visitsCountMap.get[john] ]; // 123
4 luôn theo thứ tự chèn, vì vậy chúng tôi không thể nói rằng các tập hợp này không có thứ tự, nhưng chúng tôi không thể sắp xếp lại các phần tử hoặc lấy trực tiếp một phần tử theo số của nó

Hashmap có phải là một đối tượng JavaScript không?

Hashmap cung cấp cùng chức năng khóa/giá trị và có sẵn trong JavaScript [ES6] ở dạng đối tượng Map[] [không phải .

Bản đồ khác với đối tượng trong JavaScript như thế nào?

Bản đồ được sắp xếp theo thứ tự và có thể lặp lại, trong khi đối tượng không được sắp xếp theo thứ tự và không thể lặp lại [theo nghĩa là chúng không có [Biểu tượng. iterator] thuộc tính. Tuy nhiên, bạn có thể lặp lại các phím bằng cách sử dụng for. theo cú pháp. ] Chúng ta có thể đặt bất kỳ loại dữ liệu nào làm khóa Bản đồ, trong khi các đối tượng chỉ có thể có một số, chuỗi hoặc ký hiệu làm khóa.

Bản đồ có thể được sử dụng trên đối tượng JavaScript không?

Bản đồ chứa các cặp khóa-giá trị trong đó các khóa có thể là bất kỳ kiểu dữ liệu nào. Bản đồ ghi nhớ thứ tự chèn ban đầu của các phím. Bản đồ có một thuộc tính đại diện cho kích thước của bản đồ. . Sự khác biệt giữa Đối tượng JavaScript và Bản đồ

Bản đồ có phải là một loại dữ liệu trong JavaScript không?

Bản đồ là tập hợp các mục dữ liệu có khóa , giống như Đối tượng. Nhưng sự khác biệt chính là Map cho phép các loại khóa.

Chủ Đề