Array.prototype.keys()
Baseline Widely available
This feature is well established and works across many devices and browser versions. It’s been available across browsers since May 2018.
La méthode keys()
renvoie un nouvel objet Array Iterator
qui contient les clefs pour chaque indice du tableau.
Exemple interactif
const array1 = ["a", "b", "c"];
const iterator = array1.keys();
for (const key of iterator) {
console.log(key);
}
// Expected output: 0
// Expected output: 1
// Expected output: 2
Syntaxe
js
arr.keys();
Valeur de retour
Un nouvel objet itérateur pour Array
.
Exemples
Utilisation simple
js
var arr = ["a", "b", "c"];
var itérateur = arr.keys();
console.log(itérateur.next()); // { value: 0, done: false }
console.log(itérateur.next()); // { value: 1, done: false }
console.log(itérateur.next()); // { value: 2, done: false }
console.log(itérateur.next()); // { value: undefined, done: true }
Un itérateur de clés prend en compte les trous
js
var arr = ["a", , "c"];
var clésCreuses = Object.keys(arr);
var clésDenses = [...arr.keys()];
console.log(clésCreuses); // ["0", "2"]
console.log(clésDenses); // [0, 1, 2]
Spécifications
Specification |
---|
ECMAScript® 2025 Language Specification # sec-array.prototype.keys |
Compatibilité des navigateurs
Report problems with this compatibility data on GitHubdesktop | mobile | server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
keys |
Legend
Tip: you can click/tap on a cell for more information.
- Full support
- Full support
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.