1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
---
title: Array.prototype.keys()
slug: Web/JavaScript/Reference/Global_Objects/Array/keys
tags:
- Array
- ECMAScript 2015
- Iterator
- JavaScript
- Méthode
- Prototype
- Reference
translation_of: Web/JavaScript/Reference/Global_Objects/Array/keys
original_slug: Web/JavaScript/Reference/Objets_globaux/Array/keys
---
{{JSRef}}
La méthode **`keys()`** renvoie un nouvel objet **`Array Iterator`** qui contient les clefs pour chaque indice du tableau.
{{EmbedInteractiveExample("pages/js/array-keys.html")}}
## Syntaxe
arr.keys()
### Valeur de retour
Un nouvel objet itérateur pour {{jsxref("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
| Spécification | État | Commentaires |
| ---------------------------------------------------------------------------------------------------- | ---------------------------- | -------------------- |
| {{SpecName('ES2015', '#sec-array.prototype.keys', 'Array.prototype.keys')}} | {{Spec2('ES2015')}} | Définition initiale. |
| {{SpecName('ESDraft', '#sec-array.prototype.keys', 'Array.prototype.keys')}} | {{Spec2('ESDraft')}} | |
## Compatibilité des navigateurs
{{Compat("javascript.builtins.Array.keys")}}
## Voir aussi
- {{jsxref("Array.prototype.entries()")}}
- {{jsxref("Array.prototype.values()")}}
- [Les protocoles d'itération](/fr/docs/Web/JavaScript/Reference/Les_protocoles_iteration)
|