JavaScript中的键控集合
键控集合是有序的、按照键而不是索引组织的数据集合。它们本质上是关联的。Map和set对象是键控集合,可以按照插入顺序进行迭代。
以下是JavaScript中键控集合的代码 -
示例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Document</title> <style> body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .result { font-size: 20px; font-weight: 500; color: blueviolet; } </style> </head> <body> <h1>Keyed collections in JavaScript</h1> <div class="result"></div> <br /> <button class="Btn">CLICK HERE</button> <h3>Click on the above button to create and display the object</h3> <script> let resEle = document.querySelector(".result"); let BtnEle = document.querySelector(".Btn"); BtnEle.addEventListener("click", () => { let obj = { name:'Rohan', age:22, occupation:'Student', }; for(i in obj){ resEle.innerHTML += `key = ${i} : value = ${obj[i]} <br>`; } }); </script> </body> </html>
输出
单击“点击此处”按钮后 -
广告