Node.js 节点映射可以通过键调用,但不能通过forEach()调用

Node.js 节点映射可以通过键调用,但不能通过forEach()调用,node.js,for-loop,debugging,Node.js,For Loop,Debugging,我有以下代码,允许我将特定的Map()键记录到控制台,但无法使用for each生成任何输出: let teamlist = new Map() //code to populate the map console.log(teamlist) //Shows entire map and data in console console.log(teamlist[0]) //Shows one entry from the map teamlist.forEach(team =>{

我有以下代码,允许我将特定的Map()键记录到控制台,但无法使用for each生成任何输出:

let teamlist = new Map()

//code to populate the map

console.log(teamlist) //Shows entire map and data in console
console.log(teamlist[0]) //Shows one entry from the map
teamlist.forEach(team =>{
    console.log(team) //Shows no console output, not even "undefined"
})

我认为这将是一个问题,由于异步,但没有意义,因为我可以调用单个键?我不能在地图上调用forEach()吗?

如果没有正确地将项目添加到
地图中,可能会出现这种情况

例如,如果您这样做:

let teamlist = new Map();
teamlist[0] = "hello";
然后,您所做的是将
[0]
属性添加到teamlist对象,而没有将条目添加到
映射本身。请记住,每个
Map
对象也是一个常规对象,可以具有不属于
Map
特定数据结构的常规属性。要将项目添加到
映射
对象,必须使用
.set()
方法,如下所示:

let teamlist=newmap();
temlist.set(“一些问候语”、“你好”);
temlist.set(“另一个问候语”、“再见”);
teamlist.forEach(团队=>{
控制台日志(团队);

});你完全正确。我使用的是文档中关于不做什么的例子,而不是实际做什么