array.keys()是JavaScript中的內置函數,用於返回新的數組迭代器,該迭代器包含給定輸入數組中每個索引的鍵。
用法:
array.keys()
參數:它不需要任何參數。
返回值:它返回一個新的數組迭代器。
代碼1:
<script>
// Taking input as an array A
// containing some elements.
var A = [ 'gfg', 'geeks', 'cse', 'geekpro' ];
// array.keys() function is called
var iterator = A.keys();
// printing index array using the iterator
for (let key of iterator) {
document.write(key + ' ');
}
</script>
輸出:
0 1 2 3
代碼2:
<script>
// Taking input as an array A
// containing some elements
var A = [ 'gfg', 'geeks', 'cse', 'geekpro', '', 1, 2 ];
// array.keys() function is called
var iterator = A.keys();
// printing index array using the iterator
for (let key of iterator) {
document.write(key + ' ');
}
</script>
輸出:
0 1 2 3 4 5 6
注:本文由純淨天空篩選整理自Kanchan_Ray大神的英文原創作品 JavaScript | array.keys()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。