Метод entries()
повертає новий об'єкт ітератора масиву (Array Iterator
), який містить пари ключ-значення для кожного індексу в масиві.
The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.
Синтаксис
array.entries()
Значення, яке повертається
Новий об'єкт ітератора масиву
.
Приклади
Ітерування за індексом та елементом
const a = ['a', 'b', 'c'];
for (const [index, element] of a.entries())
console.log(index, element);
// 0 'a'
// 1 'b'
// 2 'c'
Використання циклу for…of
var a = ['a', 'b', 'c'];
var iterator = a.entries();
for (let e of iterator) {
console.log(e);
}
// [0, 'a']
// [1, 'b']
// [2, 'c']
Специфікації
Специфікація | Статус | Коментар |
---|---|---|
ECMAScript 2015 (6th Edition, ECMA-262) The definition of 'Array.prototype.entries' in that specification. |
Standard | Початкове визначення. |
ECMAScript (ECMA-262) The definition of 'Array.prototype.entries' in that specification. |
Living Standard |
Сумісність з веб-переглядачами
BCD tables only load in the browser
The compatibility table in 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.