-
Notifications
You must be signed in to change notification settings - Fork 22.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Array Iterator global object #35728
Changes from all commits
1781c84
c05b7f0
a7a6c33
00eab7f
ddf38ff
73b39d6
c7752a7
eb9e5a6
26dfd06
f39e057
ce0b2d8
c815eec
5d586b8
bfcac0f
2023afc
ffaf77c
50f9823
6b3f4ee
78ec284
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,141 @@ | ||||||||||||||||||||||||||||
--- | ||||||||||||||||||||||||||||
title: Array Iterator | ||||||||||||||||||||||||||||
slug: Web/JavaScript/Reference/Global_Objects/Array_Iterator | ||||||||||||||||||||||||||||
page-type: javascript-class | ||||||||||||||||||||||||||||
browser-compat: javascript.builtins.Iterator | ||||||||||||||||||||||||||||
--- | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
{{JSRef}} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
An **`Array Iterator`** object is an object that conforms to the [iterator protocol](/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterator_protocol) by providing a `next()` method that returns an iterator result object. All Array Iterators inherit from a hidden prototype object `ArrayIteratorPrototype` which in turn inherits from the `Iterator` class. The `Iterator` class provides a [`[Symbol.iterator]()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Iterator/Symbol.iterator) method that returns the iterator object itself, making the iterator also [iterable](/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_iterable_protocol). It also provides some helper methods for working with iterators. | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
## Description | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
The `Array Iterator` is returned by [`Array.prototype.values()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/values), [`Array.prototype.keys()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/keys), [`Array.prototype.entries()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/entries), [`Array.prototype[Symbol.iterator]()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Symbol.iterator), [`TypedArray.prototype.values()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/values), [`TypedArray.prototype.keys()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/keys), [`TypedArray.prototype.entries()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/entries), [`TypedArray.prototype[Symbol.iterator]()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/Symbol.iterator), and [`arguments[Symbol.iterator]()`](/en-US/docs/Web/JavaScript/Reference/Functions/arguments/Symbol.iterator). For [`Arrays`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array), [`[Symbol.iterator]`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Symbol.iterator) is equivalent to [`values`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/values). | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
Most [Array-Like](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#array-like_objects) objects such as [`NodeList`](/en-US/docs/Web/API/NodeList) return an `Array Iterator` from their respective methods `keys()`, `values()`, `entries()`, and `[Symbol.iterator]()`. | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
> **Note:** Some Array-Like objects such as [`HTMLCollection`](/en-US/docs/Web/API/HTMLCollection) do not have a reference to the `Array Iterator` object. | ||||||||||||||||||||||||||||
Comment on lines
+17
to
+18
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [mdn-linter] reported by reviewdog 🐶
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [markdownlint] reported by reviewdog 🐶 |
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
## Constructor | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
_Inherits constructor from its ancestor_ | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [markdownlint] reported by reviewdog 🐶 |
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
- {{jsxref("Iterator/Iterator", "Iterator")}} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
Array Iterators are not instantiated via direct call of a constructor but from one of the associated Array methods. | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
## Instance properties | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
_Also inherits instance properties from its ancestor {{jsxref("Iterator")}}_. | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
## Instance methods | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
_Inherits instance methods from its ancestor {{jsxref("Iterator")}}_. | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
## Examples | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
#### HTMLCollection Iterator | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [markdownlint] reported by reviewdog 🐶 |
||||||||||||||||||||||||||||
Retrieve an `Array Iterator` from Array-Like objects with no iterator methods. This is possible through the use of [`Function.prototype.call()`](/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call) to call array methods on array-like objects. | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [mdn-linter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||
```js | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [mdn-linter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||
// Create an HTMLCollection containing all elements in the document | ||||||||||||||||||||||||||||
const collection = document.getElementsByTagName('*'); | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [mdn-linter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||
// Call the array values method with collection as this | ||||||||||||||||||||||||||||
const arrValues = [].values.call(collection); | ||||||||||||||||||||||||||||
for (const value of arrValues) { | ||||||||||||||||||||||||||||
console.log(value); | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
// Also works with the other Array->Iterator methods | ||||||||||||||||||||||||||||
const arrKeys = [].keys.call(collection); | ||||||||||||||||||||||||||||
const arrEntries = [].entries.call(collection); | ||||||||||||||||||||||||||||
const arrIter = [][Symbol.iterator].call(collection); | ||||||||||||||||||||||||||||
``` | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [mdn-linter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||
### Generating a range of values | ||||||||||||||||||||||||||||
The keys of an array are a sequence of numbers starting with 0. Using this with an iterator lets us create a range of values. | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [mdn-linter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
```js | ||||||||||||||||||||||||||||
const range = [...Array(3).keys()]; | ||||||||||||||||||||||||||||
console.log(range);//[0,1,2] | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [mdn-linter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||
``` | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
For a generalized range: | ||||||||||||||||||||||||||||
```js | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [mdn-linter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||
const makeRange = (start,end) => { | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [mdn-linter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||
const dif = end - start; | ||||||||||||||||||||||||||||
const range = [...Array(dif + 1).keys()]; | ||||||||||||||||||||||||||||
return range.map(x => x + start); | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
Comment on lines
+68
to
+69
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [mdn-linter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
makeRange(3,7);//[3,4,5,6,7] | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [mdn-linter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||
``` | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [mdn-linter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||
### Advanced Usage: Copy an Iterator | ||||||||||||||||||||||||||||
Typically when iterating through values, the iterator is consumed in the process. | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [mdn-linter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
```js | ||||||||||||||||||||||||||||
const arr = [1,2,3]; | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [mdn-linter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||
const arrIter = arr.values(); | ||||||||||||||||||||||||||||
const arrIterCopy = arrIter; | ||||||||||||||||||||||||||||
console.log([...arrIter]);//[1,2,3] | ||||||||||||||||||||||||||||
console.log([...arrIterCopy]);//[] | ||||||||||||||||||||||||||||
Comment on lines
+82
to
+83
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [mdn-linter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||
``` | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [mdn-linter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||
To get an actual copy we need to create another iterator from the source | ||||||||||||||||||||||||||||
```js | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [mdn-linter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||
const arr = [1,2,3]; | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [mdn-linter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||
const arrIter = arr.values(); | ||||||||||||||||||||||||||||
const arrIterCopy = arr.values(); | ||||||||||||||||||||||||||||
console.log([...arrIter]);//[1,2,3] | ||||||||||||||||||||||||||||
console.log([...arrIterCopy]);//[1,2,3] | ||||||||||||||||||||||||||||
Comment on lines
+92
to
+93
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [mdn-linter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||
``` | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [mdn-linter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||
In scenarios where a reference to the original array is not available, there are methods to copy an array iterator without consuming it. | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [mdn-linter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||
```js | ||||||||||||||||||||||||||||
const copyArrIter = function copyArrIter(arrIter){ | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [mdn-linter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||
// Method used to redefine object properties | ||||||||||||||||||||||||||||
const objDefProp = (obj, prop, def) => Object.defineProperty(obj, prop, { | ||||||||||||||||||||||||||||
value: def, | ||||||||||||||||||||||||||||
writable: true, | ||||||||||||||||||||||||||||
enumerable: false, | ||||||||||||||||||||||||||||
configurable: true, | ||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||
Comment on lines
+101
to
+106
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [mdn-linter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||
// Creating an array temporarily consumes the original array iterator | ||||||||||||||||||||||||||||
const arr = [...arrIter]; | ||||||||||||||||||||||||||||
// Create a copy to replace the consumed iterator | ||||||||||||||||||||||||||||
const newArrIter = arr.values(); | ||||||||||||||||||||||||||||
// By replacing next and Symbol.iterator, the original is effectively restored | ||||||||||||||||||||||||||||
objDefProp(arrIter, "next", function next() { | ||||||||||||||||||||||||||||
return newArrIter.next(); | ||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||
objDefProp(arrIter, Symbol.iterator, function iterator() { | ||||||||||||||||||||||||||||
return newArrIter; | ||||||||||||||||||||||||||||
}); | ||||||||||||||||||||||||||||
// Return a copy using the reference array | ||||||||||||||||||||||||||||
return arr.values(); | ||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [mdn-linter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
const arrIter = [1,2,3,4].values(); | ||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [mdn-linter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||
const arrIterCopy = copyArrIter(arrIter); | ||||||||||||||||||||||||||||
console.log([...arrIter]);//[1,2,3,4] | ||||||||||||||||||||||||||||
console.log([...arrIterCopy]);//[1,2,3,4] | ||||||||||||||||||||||||||||
Comment on lines
+124
to
+125
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [mdn-linter] reported by reviewdog 🐶
Suggested change
|
||||||||||||||||||||||||||||
``` | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
## Specifications | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
{{Specifications}} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
## Browser compatibility | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
{{Compat}} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
## See also | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
- {{jsxref("Iterator")}} | ||||||||||||||||||||||||||||
- {{jsxref("Array")}} | ||||||||||||||||||||||||||||
- {{jsxref("Symbol/iterator")}} | ||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [mdn-linter] reported by reviewdog 🐶
Suggested change
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[mdn-linter] reported by reviewdog 🐶