W3School TIY Editor
W3School 在线教程
改变方向
暗黑模式
运行代码
<!DOCTYPE html> <html> <body> <h1>JavaScript Map 对象</h1> <p>可以将 Array 传递给 new Map() 构造函数</p> <pre id="demo"></pre> <script> function stringify(mapObj) { var mapString = "Map对象一共有 " + mapObj.size + "个元素:<br /><br />"; mapObj.forEach( function( element , key ){ mapString += JSON.stringify(key) + ":" + element + "<br />"; }); return mapString; } const apples = {name: 'Apples'}; const bananas = {name: 'Bananas'}; const oranges = {name: 'Oranges'}; // 创建新的 Map const fruits = new Map([ [apples, 500], [bananas, 300], [oranges, 200] ]); document.getElementById("demo").innerHTML = stringify(fruits); </script> </body> </html>