Hướng dẫn javascript use object as key in map - javascript sử dụng đối tượng làm chìa khóa trong bản đồ

Đối tượng

wrongMap.has('bla')    // false
wrongMap.delete('bla') // false
console.log(wrongMap)  // Map { bla: 'blaa', bla2: 'blaaa2' }
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.

Show

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 () {}
2

Tạ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 () {}
4

Hà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 () {}
5

Trả 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 () {}
7

Xó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 () {}
9

Trả 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"
5

Trả 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"
7

Trả 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
2

Trả 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
5

Trả 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
7

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.

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
9

Trả 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
2

Trả 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.

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.

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:

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' }
0

Thông số kỹ thuật

Sự chỉ rõ
Đặ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
    wrongMap.has('bla')    // false
    wrongMap.delete('bla') // false
    console.log(wrongMap)  // Map { bla: 'blaa', bla2: 'blaaa2' }
    
    1 có sẵn trong
    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"]
    
    
    7
  • 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"]
    
    
    8
  • 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"]
    
    
    9
  • 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)
    
    0

Khóa đối tượng có thể là một mảng không?

Sự mô tả. Object.keys () Trả về một mảng có các phần tử là các chuỗi tương ứng với các thuộc tính được tìm thấy trực tiếp trên đối tượng. Thứ tự của các thuộc tính giống như được đưa ra bằng cách lặp qua các thuộc tính của đối tượng theo cách thủ công.Object.keys() returns an array whose elements are strings corresponding to the enumerable properties found directly upon object . The ordering of the properties is the same as that given by looping over the properties of the object manually.

Làm thế nào để bạn truy cập khóa của một đối tượng?

Làm thế nào để có được các khóa, giá trị và mục nhập trong đối tượng JavaScript ?..
Object.Keys (OBJ) - Trả về tất cả các khóa của đối tượng dưới dạng mảng ..
Object.Values (OBJ) - Trả về tất cả các giá trị của đối tượng dưới dạng mảng ..
Object.entries (OBJ) - Trả về một mảng [khóa, giá trị].

Bản đồ có cho phép các khóa trùng lặp JavaScript không?

Khóa bản đồ.Bản đồ chấp nhận bất kỳ loại dữ liệu nào làm khóa và không cho phép các giá trị khóa trùng lặp.do not allow duplicate key values.

Làm thế nào để bạn lặp lại một đối tượng?

Cách lặp lại các thuộc tính đối tượng trong JavaScript..
const items = {'đầu tiên': ngày mới (), 'thứ hai': 2, 'thứ ba': 'test'}.
mặt hàng.Bản đồ (Mục => {}).
mặt hàng.foreach (item => {}).
for (const item của các mục) {}.
for (const item trong các mục) {bảng điều khiển.log (mục)}.
Sự vật.Mục nhập (mục).Bản đồ (item => {console. log (item)}) đối tượng ..