在JavaScript中,遍历一个map对象有以下几种方法:
const myMap = new Map([['a', 1], ['b', 2], ['c', 3]]);
for (let [key, value] of myMap) {
console.log(`${key} = ${value}`);
}
const myMap = new Map([['a', 1], ['b', 2], ['c', 3]]);
myMap.forEach((value, key) => {
console.log(`${key} = ${value}`);
});
const myMap = new Map([['a', 1], ['b', 2], ['c', 3]]);
for (let [key, value] of myMap.entries()) {
console.log(`${key} = ${value}`);
}
const myMap = new Map([['a', 1], ['b', 2], ['c', 3]]);
for (let key of myMap.keys()) {
console.log(key);
}
for (let value of myMap.values()) {
console.log(value);
}
这些方法可以根据具体的需求选择使用,以便轻松遍历map对象的键、值或键值对。