JavaScript array.keys() 方法


JavaScript array.keys() 建立一個數組迭代器物件,其中只包含一個數組的鍵

下面是 array.keys() 方法的程式碼 −

示例

 線上演示

<!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;
   }
   .sample {
      font-size: 20px;
      font-weight: 500;
   }
</style>
</head>
<body>
<h1>JavaScript array keys()</h1>
<div class="sample"></div>
<button class="Btn">CLICK HERE</button>
<h3>
Click on the above button to see the keys of the array
</h3>
<script>
   let fillEle = document.querySelector(".sample");
   let arr = ["H", "E", "L", "L", "O"];
   fillEle.innerHTML = arr;
   arr = arr.keys();
   document.querySelector(".Btn").addEventListener("click", () => {
      fillEle.innerHTML = "";
      for (let x of arr) {
         fillEle.innerHTML += x + "<br>";
      }
   });
</script>
</body>
</html>

輸出

單擊“點選此處”按鈕 −


更新於: 2020 年 5 月 8 日

115 次瀏覽

開啟你的職業生涯

完成課程認證

立即開始
廣告
© . All rights reserved.