Map.prototype.keys()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

Phương thức keys() của các instance Map trả về một đối tượng map iterator mới chứa các key cho mỗi phần tử trong map này theo thứ tự chèn.

Try it

const map = new Map();

map.set("0", "foo");
map.set(1, "bar");

const iterator = map.keys();

console.log(iterator.next().value);
// Expected output: "0"

console.log(iterator.next().value);
// Expected output: 1

Cú pháp

js
keys()

Tham số

Không có.

Giá trị trả về

Một đối tượng iterator có thể lặp mới.

Ví dụ

Sử dụng keys()

js
const myMap = new Map();
myMap.set("0", "foo");
myMap.set(1, "bar");
myMap.set({}, "baz");

const mapIter = myMap.keys();

console.log(mapIter.next().value); // "0"
console.log(mapIter.next().value); // 1
console.log(mapIter.next().value); // {}

Đặc tả kỹ thuật

Specification
ECMAScript® 2027 Language Specification
# sec-map.prototype.keys

Tương thích trình duyệt

Xem thêm