Đối tượng
1 giữ các cặp giá trị khóa và nhớ thứ tự chèn ban đầu của các khóa. Bất kỳ giá trị nào [cả đối tượng và giá trị nguyên thủy] có thể được sử dụng làm khóa hoặc giá trị.wrongMap.has['bla'] // false
wrongMap.delete['bla'] // false
console.log[wrongMap] // Map { bla: 'blaa', bla2: 'blaaa2' }
1 object holds key-value pairs and remembers the original insertion order of the keys. Any value [both objects and primitive values] may be used as either a key or a value. wrongMap.has['bla'] // false
wrongMap.delete['bla'] // false
console.log[wrongMap] // Map { bla: 'blaa', bla2: 'blaaa2' }
Thử nó
Sự mô tả
Đối tượng
wrongMap.has['bla'] // false
wrongMap.delete['bla'] // false
console.log[wrongMap] // Map { bla: 'blaa', bla2: 'blaaa2' }
1 là bộ sưu tập của các cặp giá trị khóa. Một chìa khóa trong wrongMap.has['bla'] // false
wrongMap.delete['bla'] // false
console.log[wrongMap] // Map { bla: 'blaa', bla2: 'blaaa2' }
1 chỉ có thể xảy ra một lần; Nó là duy nhất trong bộ sưu tập của ____ 11. Một đối tượng wrongMap.has['bla'] // false
wrongMap.delete['bla'] // false
console.log[wrongMap] // Map { bla: 'blaa', bla2: 'blaaa2' }
1 được lặp lại bởi các cặp giá trị khóa-vòng lặp wrongMap.has['bla'] // false
wrongMap.delete['bla'] // false
console.log[wrongMap] // Map { bla: 'blaa', bla2: 'blaaa2' }
6 trả về một mảng 2 thành viên wrongMap.has['bla'] // false
wrongMap.delete['bla'] // false
console.log[wrongMap] // Map { bla: 'blaa', bla2: 'blaaa2' }
7 cho mỗi lần lặp. Lặp lại xảy ra theo thứ tự chèn, tương ứng với thứ tự trong đó mỗi cặp giá trị khóa lần đầu tiên được chèn vào bản đồ bằng phương thức wrongMap.has['bla'] // false
wrongMap.delete['bla'] // false
console.log[wrongMap] // Map { bla: 'blaa', bla2: 'blaaa2' }
8 [nghĩa là không có khóa có cùng giá trị trong bản đồ khi wrongMap.has['bla'] // false
wrongMap.delete['bla'] // false
console.log[wrongMap] // Map { bla: 'blaa', bla2: 'blaaa2' }
8 được gọi là ].may
only occur once; it is unique in the wrongMap.has['bla'] // false
wrongMap.delete['bla'] // false
console.log[wrongMap] // Map { bla: 'blaa', bla2: 'blaaa2' }
1's collection. A wrongMap.has['bla'] // false
wrongMap.delete['bla'] // false
console.log[wrongMap] // Map { bla: 'blaa', bla2: 'blaaa2' }
1 object is iterated by key-value pairs — a wrongMap.has['bla'] // false
wrongMap.delete['bla'] // false
console.log[wrongMap] // Map { bla: 'blaa', bla2: 'blaaa2' }
6 loop returns a 2-member array of wrongMap.has['bla'] // false
wrongMap.delete['bla'] // false
console.log[wrongMap] // Map { bla: 'blaa', bla2: 'blaaa2' }
7 for each iteration. Iteration happens in insertion order, which corresponds to the order in which each key-value pair was first inserted into the map by the
wrongMap.has['bla'] // false
wrongMap.delete['bla'] // false
console.log[wrongMap] // Map { bla: 'blaa', bla2: 'blaaa2' }
8 method [that is, there wasn't a key with the same value already in the map when wrongMap.has['bla'] // false
wrongMap.delete['bla'] // false
console.log[wrongMap] // Map { bla: 'blaa', bla2: 'blaaa2' }
8 was called].Đặc điểm kỹ thuật yêu cầu các bản đồ phải được thực hiện "Trung bình, cung cấp thời gian truy cập là cao cấp về số lượng các phần tử trong bộ sưu tập". Do đó, nó có thể được biểu diễn bên trong dưới dạng bảng băm [với o [1] tra cứu], một cây tìm kiếm [với o [log [n]] tra cứu] hoặc bất kỳ cấu trúc dữ liệu nào khác, miễn là độ phức tạp tốt hơn o [N].
Bình đẳng chính
Bình đẳng giá trị dựa trên thuật toán sikevaluezero. .
Đối tượng so với bản đồ
const contacts = new Map[]
contacts.set['Jessie', {phone: "213-555-1234", address: "123 N 1st Ave"}]
contacts.has['Jessie'] // true
contacts.get['Hilary'] // undefined
contacts.set['Hilary', {phone: "617-555-4321", address: "321 S 2nd St"}]
contacts.get['Jessie'] // {phone: "213-555-1234", address: "123 N 1st Ave"}
contacts.delete['Raymond'] // false
contacts.delete['Jessie'] // true
console.log[contacts.size] // 1
6 tương tự như ________ 11, cả hai cho phép bạn đặt các khóa thành các giá trị, truy xuất các giá trị đó, xóa các khóa và phát hiện xem có gì đó được lưu trữ ở một phím không. Vì lý do này [và vì không có lựa chọn thay thế tích hợp], const contacts = new Map[]
contacts.set['Jessie', {phone: "213-555-1234", address: "123 N 1st Ave"}]
contacts.has['Jessie'] // true
contacts.get['Hilary'] // undefined
contacts.set['Hilary', {phone: "617-555-4321", address: "321 S 2nd St"}]
contacts.get['Jessie'] // {phone: "213-555-1234", address: "123 N 1st Ave"}
contacts.delete['Raymond'] // false
contacts.delete['Jessie'] // true
console.log[contacts.size] // 1
6 đã được sử dụng như wrongMap.has['bla'] // false
wrongMap.delete['bla'] // false
console.log[wrongMap] // Map { bla: 'blaa', bla2: 'blaaa2' }
1 trong lịch sử.Tuy nhiên, có những khác biệt quan trọng làm cho
wrongMap.has['bla'] // false
wrongMap.delete['bla'] // false
console.log[wrongMap] // Map { bla: 'blaa', bla2: 'blaaa2' }
1 thích hợp hơn trong một số trường hợp:Đặt thuộc tính đối tượng
Cài đặt các thuộc tính đối tượng cũng hoạt động cho các đối tượng MAP và có thể gây ra sự nhầm lẫn đáng kể.
Do đó, điều này dường như hoạt động theo một cách:
const wrongMap = new Map[];
wrongMap['bla'] = 'blaa';
wrongMap['bla2'] = 'blaaa2';
console.log[wrongMap]; // Map { bla: 'blaa', bla2: 'blaaa2' }
Nhưng cách đặt một thuộc tính không tương tác với cấu trúc dữ liệu bản đồ. Nó sử dụng tính năng của đối tượng chung. Giá trị của 'BLA' không được lưu trữ trong bản đồ cho các truy vấn. Các hoạt động khác trên dữ liệu không thành công:
wrongMap.has['bla'] // false
wrongMap.delete['bla'] // false
console.log[wrongMap] // Map { bla: 'blaa', bla2: 'blaaa2' }
Việc sử dụng chính xác để lưu trữ dữ liệu trong bản đồ là thông qua phương thức
const myMap = new Map[];
const keyString = 'a string';
const keyObj = {};
const keyFunc = function[] {};
// setting the values
myMap.set[keyString, "value associated with 'a string'"];
myMap.set[keyObj, 'value associated with keyObj'];
myMap.set[keyFunc, 'value associated with keyFunc'];
console.log[myMap.size]; // 3
// getting the values
console.log[myMap.get[keyString]]; // "value associated with 'a string'"
console.log[myMap.get[keyObj]]; // "value associated with keyObj"
console.log[myMap.get[keyFunc]]; // "value associated with keyFunc"
console.log[myMap.get['a string']]; // "value associated with 'a string'", because keyString === 'a string'
console.log[myMap.get[{}]]; // undefined, because keyObj !== {}
console.log[myMap.get[function[] {}]]; // undefined, because keyFunc !== function [] {}
1.const contacts = new Map[]
contacts.set['Jessie', {phone: "213-555-1234", address: "123 N 1st Ave"}]
contacts.has['Jessie'] // true
contacts.get['Hilary'] // undefined
contacts.set['Hilary', {phone: "617-555-4321", address: "321 S 2nd St"}]
contacts.get['Jessie'] // {phone: "213-555-1234", address: "123 N 1st Ave"}
contacts.delete['Raymond'] // false
contacts.delete['Jessie'] // true
console.log[contacts.size] // 1
Người xây dựng
const myMap = new Map[];
const keyString = 'a string';
const keyObj = {};
const keyFunc = function[] {};
// setting the values
myMap.set[keyString, "value associated with 'a string'"];
myMap.set[keyObj, 'value associated with keyObj'];
myMap.set[keyFunc, 'value associated with keyFunc'];
console.log[myMap.size]; // 3
// getting the values
console.log[myMap.get[keyString]]; // "value associated with 'a string'"
console.log[myMap.get[keyObj]]; // "value associated with keyObj"
console.log[myMap.get[keyFunc]]; // "value associated with keyFunc"
console.log[myMap.get['a string']]; // "value associated with 'a string'", because keyString === 'a string'
console.log[myMap.get[{}]]; // undefined, because keyObj !== {}
console.log[myMap.get[function[] {}]]; // undefined, because keyFunc !== function [] {}
2Tạo một đối tượng
wrongMap.has['bla'] // false
wrongMap.delete['bla'] // false
console.log[wrongMap] // Map { bla: 'blaa', bla2: 'blaaa2' }
1 mới.Tính chất tĩnh
const myMap = new Map[];
const keyString = 'a string';
const keyObj = {};
const keyFunc = function[] {};
// setting the values
myMap.set[keyString, "value associated with 'a string'"];
myMap.set[keyObj, 'value associated with keyObj'];
myMap.set[keyFunc, 'value associated with keyFunc'];
console.log[myMap.size]; // 3
// getting the values
console.log[myMap.get[keyString]]; // "value associated with 'a string'"
console.log[myMap.get[keyObj]]; // "value associated with keyObj"
console.log[myMap.get[keyFunc]]; // "value associated with keyFunc"
console.log[myMap.get['a string']]; // "value associated with 'a string'", because keyString === 'a string'
console.log[myMap.get[{}]]; // undefined, because keyObj !== {}
console.log[myMap.get[function[] {}]]; // undefined, because keyFunc !== function [] {}
4Hàm tạo thành được sử dụng để tạo các đối tượng dẫn xuất.
Thuộc tính thể hiện
const myMap = new Map[];
const keyString = 'a string';
const keyObj = {};
const keyFunc = function[] {};
// setting the values
myMap.set[keyString, "value associated with 'a string'"];
myMap.set[keyObj, 'value associated with keyObj'];
myMap.set[keyFunc, 'value associated with keyFunc'];
console.log[myMap.size]; // 3
// getting the values
console.log[myMap.get[keyString]]; // "value associated with 'a string'"
console.log[myMap.get[keyObj]]; // "value associated with keyObj"
console.log[myMap.get[keyFunc]]; // "value associated with keyFunc"
console.log[myMap.get['a string']]; // "value associated with 'a string'", because keyString === 'a string'
console.log[myMap.get[{}]]; // undefined, because keyObj !== {}
console.log[myMap.get[function[] {}]]; // undefined, because keyFunc !== function [] {}
5Trả về số cặp khóa/giá trị trong đối tượng
wrongMap.has['bla'] // false
wrongMap.delete['bla'] // false
console.log[wrongMap] // Map { bla: 'blaa', bla2: 'blaaa2' }
1.Phương pháp thể hiện
const myMap = new Map[];
const keyString = 'a string';
const keyObj = {};
const keyFunc = function[] {};
// setting the values
myMap.set[keyString, "value associated with 'a string'"];
myMap.set[keyObj, 'value associated with keyObj'];
myMap.set[keyFunc, 'value associated with keyFunc'];
console.log[myMap.size]; // 3
// getting the values
console.log[myMap.get[keyString]]; // "value associated with 'a string'"
console.log[myMap.get[keyObj]]; // "value associated with keyObj"
console.log[myMap.get[keyFunc]]; // "value associated with keyFunc"
console.log[myMap.get['a string']]; // "value associated with 'a string'", because keyString === 'a string'
console.log[myMap.get[{}]]; // undefined, because keyObj !== {}
console.log[myMap.get[function[] {}]]; // undefined, because keyFunc !== function [] {}
7Xóa tất cả các cặp giá trị khóa khỏi đối tượng
wrongMap.has['bla'] // false
wrongMap.delete['bla'] // false
console.log[wrongMap] // Map { bla: 'blaa', bla2: 'blaaa2' }
1.const myMap = new Map[];
const keyString = 'a string';
const keyObj = {};
const keyFunc = function[] {};
// setting the values
myMap.set[keyString, "value associated with 'a string'"];
myMap.set[keyObj, 'value associated with keyObj'];
myMap.set[keyFunc, 'value associated with keyFunc'];
console.log[myMap.size]; // 3
// getting the values
console.log[myMap.get[keyString]]; // "value associated with 'a string'"
console.log[myMap.get[keyObj]]; // "value associated with keyObj"
console.log[myMap.get[keyFunc]]; // "value associated with keyFunc"
console.log[myMap.get['a string']]; // "value associated with 'a string'", because keyString === 'a string'
console.log[myMap.get[{}]]; // undefined, because keyObj !== {}
console.log[myMap.get[function[] {}]]; // undefined, because keyFunc !== function [] {}
9Trả về
const myMap = new Map[];
myMap.set[NaN, 'not a number'];
myMap.get[NaN];
// "not a number"
const otherNaN = Number['foo'];
myMap.get[otherNaN];
// "not a number"
0 Nếu một phần tử trong đối tượng wrongMap.has['bla'] // false
wrongMap.delete['bla'] // false
console.log[wrongMap] // Map { bla: 'blaa', bla2: 'blaaa2' }
1 tồn tại và đã bị xóa hoặc const myMap = new Map[];
myMap.set[NaN, 'not a number'];
myMap.get[NaN];
// "not a number"
const otherNaN = Number['foo'];
myMap.get[otherNaN];
// "not a number"
2 nếu phần tử không tồn tại. const myMap = new Map[];
myMap.set[NaN, 'not a number'];
myMap.get[NaN];
// "not a number"
const otherNaN = Number['foo'];
myMap.get[otherNaN];
// "not a number"
3 sẽ trở lại const myMap = new Map[];
myMap.set[NaN, 'not a number'];
myMap.get[NaN];
// "not a number"
const otherNaN = Number['foo'];
myMap.get[otherNaN];
// "not a number"
2 sau đó.const myMap = new Map[];
myMap.set[NaN, 'not a number'];
myMap.get[NaN];
// "not a number"
const otherNaN = Number['foo'];
myMap.get[otherNaN];
// "not a number"
5Trả về giá trị được liên kết với khóa được truyền hoặc
const myMap = new Map[];
myMap.set[NaN, 'not a number'];
myMap.get[NaN];
// "not a number"
const otherNaN = Number['foo'];
myMap.get[otherNaN];
// "not a number"
6 nếu không có.const myMap = new Map[];
myMap.set[NaN, 'not a number'];
myMap.get[NaN];
// "not a number"
const otherNaN = Number['foo'];
myMap.get[otherNaN];
// "not a number"
7Trả về một boolean cho biết liệu một giá trị có được liên kết với khóa được truyền trong đối tượng
wrongMap.has['bla'] // false
wrongMap.delete['bla'] // false
console.log[wrongMap] // Map { bla: 'blaa', bla2: 'blaaa2' }
1 hay không.const myMap = new Map[];
myMap.set[NaN, 'not a number'];
myMap.get[NaN];
// "not a number"
const otherNaN = Number['foo'];
myMap.get[otherNaN];
// "not a number"
9Đặt giá trị cho khóa được truyền trong đối tượng
wrongMap.has['bla'] // false
wrongMap.delete['bla'] // false
console.log[wrongMap] // Map { bla: 'blaa', bla2: 'blaaa2' }
1. Trả về đối tượng wrongMap.has['bla'] // false
wrongMap.delete['bla'] // false
console.log[wrongMap] // Map { bla: 'blaa', bla2: 'blaaa2' }
1.const myMap = new Map[];
myMap.set[0, 'zero'];
myMap.set[1, 'one'];
for [const [key, value] of myMap] {
console.log[`${key} = ${value}`];
}
// 0 = zero
// 1 = one
for [const key of myMap.keys[]] {
console.log[key];
}
// 0
// 1
for [const value of myMap.values[]] {
console.log[value];
}
// zero
// one
for [const [key, value] of myMap.entries[]] {
console.log[`${key} = ${value}`];
}
// 0 = zero
// 1 = one
2Trả về một đối tượng lặp mới có chứa một mảng hai thành viên là
wrongMap.has['bla'] // false
wrongMap.delete['bla'] // false
console.log[wrongMap] // Map { bla: 'blaa', bla2: 'blaaa2' }
7 cho mỗi phần tử trong đối tượng wrongMap.has['bla'] // false
wrongMap.delete['bla'] // false
console.log[wrongMap] // Map { bla: 'blaa', bla2: 'blaaa2' }
1 theo thứ tự chèn.const myMap = new Map[];
myMap.set[0, 'zero'];
myMap.set[1, 'one'];
for [const [key, value] of myMap] {
console.log[`${key} = ${value}`];
}
// 0 = zero
// 1 = one
for [const key of myMap.keys[]] {
console.log[key];
}
// 0
// 1
for [const value of myMap.values[]] {
console.log[value];
}
// zero
// one
for [const [key, value] of myMap.entries[]] {
console.log[`${key} = ${value}`];
}
// 0 = zero
// 1 = one
5Trả về một đối tượng lặp mới có chứa các phím cho từng phần tử trong đối tượng
wrongMap.has['bla'] // false
wrongMap.delete['bla'] // false
console.log[wrongMap] // Map { bla: 'blaa', bla2: 'blaaa2' }
1 theo thứ tự chèn.const myMap = new Map[];
myMap.set[0, 'zero'];
myMap.set[1, 'one'];
for [const [key, value] of myMap] {
console.log[`${key} = ${value}`];
}
// 0 = zero
// 1 = one
for [const key of myMap.keys[]] {
console.log[key];
}
// 0
// 1
for [const value of myMap.values[]] {
console.log[value];
}
// zero
// one
for [const [key, value] of myMap.entries[]] {
console.log[`${key} = ${value}`];
}
// 0 = zero
// 1 = one
7Trả về một đối tượng lặp mới có chứa các giá trị cho từng phần tử trong đối tượng
wrongMap.has['bla'] // false
wrongMap.delete['bla'] // false
console.log[wrongMap] // Map { bla: 'blaa', bla2: 'blaaa2' }
1 theo thứ tự chèn.const myMap = new Map[];
myMap.set[0, 'zero'];
myMap.set[1, 'one'];
for [const [key, value] of myMap] {
console.log[`${key} = ${value}`];
}
// 0 = zero
// 1 = one
for [const key of myMap.keys[]] {
console.log[key];
}
// 0
// 1
for [const value of myMap.values[]] {
console.log[value];
}
// zero
// one
for [const [key, value] of myMap.entries[]] {
console.log[`${key} = ${value}`];
}
// 0 = zero
// 1 = one
9Trả về một đối tượng lặp mới có chứa một mảng hai thành viên là
wrongMap.has['bla'] // false
wrongMap.delete['bla'] // false
console.log[wrongMap] // Map { bla: 'blaa', bla2: 'blaaa2' }
7 cho mỗi phần tử trong đối tượng wrongMap.has['bla'] // false
wrongMap.delete['bla'] // false
console.log[wrongMap] // Map { bla: 'blaa', bla2: 'blaaa2' }
1 theo thứ tự chèn.myMap.forEach[[value, key] => {
console.log[`${key} = ${value}`];
}];
// 0 = zero
// 1 = one
2Trả về một đối tượng lặp mới có chứa các phím cho từng phần tử trong đối tượng
wrongMap.has['bla'] // false
wrongMap.delete['bla'] // false
console.log[wrongMap] // Map { bla: 'blaa', bla2: 'blaaa2' }
1 theo thứ tự chèn.Trả về một đối tượng lặp mới có chứa các giá trị cho từng phần tử trong đối tượng wrongMap.has['bla'] // false
wrongMap.delete['bla'] // false
console.log[wrongMap] // Map { bla: 'blaa', bla2: 'blaaa2' }
1 theo thứ tự chèn.
wrongMap.has['bla'] // false
wrongMap.delete['bla'] // false
console.log[wrongMap] // Map { bla: 'blaa', bla2: 'blaaa2' }
Gọi myMap.forEach[[value, key] => {
console.log[`${key} = ${value}`];
}];
// 0 = zero
// 1 = one
3 một lần cho mỗi cặp giá trị khóa có trong đối tượng wrongMap.has['bla'] // false
wrongMap.delete['bla'] // false
console.log[wrongMap] // Map { bla: 'blaa', bla2: 'blaaa2' }
1, theo thứ tự chèn. Nếu một tham số myMap.forEach[[value, key] => {
console.log[`${key} = ${value}`];
}];
// 0 = zero
// 1 = one
5 được cung cấp cho myMap.forEach[[value, key] => {
console.log[`${key} = ${value}`];
}];
// 0 = zero
// 1 = one
6, nó sẽ được sử dụng làm giá trị myMap.forEach[[value, key] => {
console.log[`${key} = ${value}`];
}];
// 0 = zero
// 1 = one
7 cho mỗi cuộc gọi lại.
myMap.forEach[[value, key] => {
console.log[`${key} = ${value}`];
}];
// 0 = zero
// 1 = one
wrongMap.has['bla'] // false
wrongMap.delete['bla'] // false
console.log[wrongMap] // Map { bla: 'blaa', bla2: 'blaaa2' }
myMap.forEach[[value, key] => {
console.log[`${key} = ${value}`];
}];
// 0 = zero
// 1 = one
myMap.forEach[[value, key] => {
console.log[`${key} = ${value}`];
}];
// 0 = zero
// 1 = one
myMap.forEach[[value, key] => {
console.log[`${key} = ${value}`];
}];
// 0 = zero
// 1 = one
const myMap = new Map[];
const keyString = 'a string';
const keyObj = {};
const keyFunc = function[] {};
// setting the values
myMap.set[keyString, "value associated with 'a string'"];
myMap.set[keyObj, 'value associated with keyObj'];
myMap.set[keyFunc, 'value associated with keyFunc'];
console.log[myMap.size]; // 3
// getting the values
console.log[myMap.get[keyString]]; // "value associated with 'a string'"
console.log[myMap.get[keyObj]]; // "value associated with keyObj"
console.log[myMap.get[keyFunc]]; // "value associated with keyFunc"
console.log[myMap.get['a string']]; // "value associated with 'a string'", because keyString === 'a string'
console.log[myMap.get[{}]]; // undefined, because keyObj !== {}
console.log[myMap.get[function[] {}]]; // undefined, because keyFunc !== function [] {}
Ví dụ
Sử dụng đối tượng bản đồ
const myMap = new Map[];
myMap.set[NaN, 'not a number'];
myMap.get[NaN];
// "not a number"
const otherNaN = Number['foo'];
myMap.get[otherNaN];
// "not a number"
Sử dụng NaN làm khóa bản đồ
const contacts = new Map[]
contacts.set['Jessie', {phone: "213-555-1234", address: "123 N 1st Ave"}]
contacts.has['Jessie'] // true
contacts.get['Hilary'] // undefined
contacts.set['Hilary', {phone: "617-555-4321", address: "321 S 2nd St"}]
contacts.get['Jessie'] // {phone: "213-555-1234", address: "123 N 1st Ave"}
contacts.delete['Raymond'] // false
contacts.delete['Jessie'] // true
console.log[contacts.size] // 1
2 cũng có thể được sử dụng làm chìa khóa. Mặc dù mọi const contacts = new Map[]
contacts.set['Jessie', {phone: "213-555-1234", address: "123 N 1st Ave"}]
contacts.has['Jessie'] // true
contacts.get['Hilary'] // undefined
contacts.set['Hilary', {phone: "617-555-4321", address: "321 S 2nd St"}]
contacts.get['Jessie'] // {phone: "213-555-1234", address: "123 N 1st Ave"}
contacts.delete['Raymond'] // false
contacts.delete['Jessie'] // true
console.log[contacts.size] // 1
2 không bằng chính nó [const contacts = new Map[]
contacts.set['Jessie', {phone: "213-555-1234", address: "123 N 1st Ave"}]
contacts.has['Jessie'] // true
contacts.get['Hilary'] // undefined
contacts.set['Hilary', {phone: "617-555-4321", address: "321 S 2nd St"}]
contacts.get['Jessie'] // {phone: "213-555-1234", address: "123 N 1st Ave"}
contacts.delete['Raymond'] // false
contacts.delete['Jessie'] // true
console.log[contacts.size] // 1
4 là đúng], ví dụ sau đây hoạt động vì const contacts = new Map[]
contacts.set['Jessie', {phone: "213-555-1234", address: "123 N 1st Ave"}]
contacts.has['Jessie'] // true
contacts.get['Hilary'] // undefined
contacts.set['Hilary', {phone: "617-555-4321", address: "321 S 2nd St"}]
contacts.get['Jessie'] // {phone: "213-555-1234", address: "123 N 1st Ave"}
contacts.delete['Raymond'] // false
contacts.delete['Jessie'] // true
console.log[contacts.size] // 1
2 không thể phân biệt được với nhau:const myMap = new Map[];
myMap.set[0, 'zero'];
myMap.set[1, 'one'];
for [const [key, value] of myMap] {
console.log[`${key} = ${value}`];
}
// 0 = zero
// 1 = one
for [const key of myMap.keys[]] {
console.log[key];
}
// 0
// 1
for [const value of myMap.values[]] {
console.log[value];
}
// zero
// one
for [const [key, value] of myMap.entries[]] {
console.log[`${key} = ${value}`];
}
// 0 = zero
// 1 = one
Lặp lại bản đồ với ... của
Bản đồ có thể được lặp lại bằng cách sử dụng vòng lặp
wrongMap.has['bla'] // false
wrongMap.delete['bla'] // false
console.log[wrongMap] // Map { bla: 'blaa', bla2: 'blaaa2' }
6:myMap.forEach[[value, key] => {
console.log[`${key} = ${value}`];
}];
// 0 = zero
// 1 = one
Lặp lại bản đồ với foreach []
const kvArray = [['key1', 'value1'], ['key2', 'value2']];
// Use the regular Map constructor to transform a 2D key-value Array into a map
const myMap = new Map[kvArray];
console.log[myMap.get['key1']]; // "value1"
// Use Array.from[] to transform a map into a 2D key-value Array
console.log[Array.from[myMap]]; // Will show you exactly the same Array as kvArray
// A succinct way to do the same, using the spread syntax
console.log[[...myMap]];
// Or use the keys[] or values[] iterators, and convert them to an array
console.log[Array.from[myMap.keys[]]]; // ["key1", "key2"]
Bản đồ có thể được lặp lại bằng phương pháp const kvArray = [['key1', 'value1'], ['key2', 'value2']];
// Use the regular Map constructor to transform a 2D key-value Array into a map
const myMap = new Map[kvArray];
console.log[myMap.get['key1']]; // "value1"
// Use Array.from[] to transform a map into a 2D key-value Array
console.log[Array.from[myMap]]; // Will show you exactly the same Array as kvArray
// A succinct way to do the same, using the spread syntax
console.log[[...myMap]];
// Or use the keys[] or values[] iterators, and convert them to an array
console.log[Array.from[myMap.keys[]]]; // ["key1", "key2"]
3:
const kvArray = [['key1', 'value1'], ['key2', 'value2']];
// Use the regular Map constructor to transform a 2D key-value Array into a map
const myMap = new Map[kvArray];
console.log[myMap.get['key1']]; // "value1"
// Use Array.from[] to transform a map into a 2D key-value Array
console.log[Array.from[myMap]]; // Will show you exactly the same Array as kvArray
// A succinct way to do the same, using the spread syntax
console.log[[...myMap]];
// Or use the keys[] or values[] iterators, and convert them to an array
console.log[Array.from[myMap.keys[]]]; // ["key1", "key2"]
Quan hệ với các đối tượng mảng
const original = new Map[[
[1, 'one'],
]];
const clone = new Map[original];
console.log[clone.get[1]]; // one
console.log[original === clone]; // false [useful for shallow comparison]
Bản đồ nhân bản và hợp nhất Keep in mind that the data itself is not cloned.
Bản đồ có thể được hợp nhất, duy trì tính duy nhất quan trọng:
const first = new Map[[
[1, 'one'],
[2, 'two'],
[3, 'three'],
]];
const second = new Map[[
[1, 'uno'],
[2, 'dos'],
]];
// Merge two maps. The last repeated key wins.
// Spread syntax essentially converts a Map to an Array
const merged = new Map[[...first, ...second]];
console.log[merged.get[1]]; // uno
console.log[merged.get[2]]; // dos
console.log[merged.get[3]]; // three
Bản đồ cũng có thể được hợp nhất với các mảng:
wrongMap.has['bla'] // false
wrongMap.delete['bla'] // false
console.log[wrongMap] // Map { bla: 'blaa', bla2: 'blaaa2' }
0Thông số kỹ thuật
Đặc tả ngôn ngữ Ecmascript # Sec-Map-Enjects # sec-map-objects |
Tính tương thích của trình duyệt web
Bảng BCD chỉ tải trong trình duyệt
Xem thêm
- Polyfill của
1 có sẵn trongwrongMap.has['bla'] // false wrongMap.delete['bla'] // false console.log[wrongMap] // Map { bla: 'blaa', bla2: 'blaaa2' }
7const kvArray = [['key1', 'value1'], ['key2', 'value2']]; // Use the regular Map constructor to transform a 2D key-value Array into a map const myMap = new Map[kvArray]; console.log[myMap.get['key1']]; // "value1" // Use Array.from[] to transform a map into a 2D key-value Array console.log[Array.from[myMap]]; // Will show you exactly the same Array as kvArray // A succinct way to do the same, using the spread syntax console.log[[...myMap]]; // Or use the keys[] or values[] iterators, and convert them to an array console.log[Array.from[myMap.keys[]]]; // ["key1", "key2"]
8const kvArray = [['key1', 'value1'], ['key2', 'value2']]; // Use the regular Map constructor to transform a 2D key-value Array into a map const myMap = new Map[kvArray]; console.log[myMap.get['key1']]; // "value1" // Use Array.from[] to transform a map into a 2D key-value Array console.log[Array.from[myMap]]; // Will show you exactly the same Array as kvArray // A succinct way to do the same, using the spread syntax console.log[[...myMap]]; // Or use the keys[] or values[] iterators, and convert them to an array console.log[Array.from[myMap.keys[]]]; // ["key1", "key2"]
9const kvArray = [['key1', 'value1'], ['key2', 'value2']]; // Use the regular Map constructor to transform a 2D key-value Array into a map const myMap = new Map[kvArray]; console.log[myMap.get['key1']]; // "value1" // Use Array.from[] to transform a map into a 2D key-value Array console.log[Array.from[myMap]]; // Will show you exactly the same Array as kvArray // A succinct way to do the same, using the spread syntax console.log[[...myMap]]; // Or use the keys[] or values[] iterators, and convert them to an array console.log[Array.from[myMap.keys[]]]; // ["key1", "key2"]
0const original = new Map[[ [1, 'one'], ]]; const clone = new Map[original]; console.log[clone.get[1]]; // one console.log[original === clone]; // false [useful for shallow comparison]