简体   繁体   中英

Convert Array to ES6 Iterable

I have found the answer to: Convert ES6 Iterable to Array

But I'm looking for the opposite :

How can I convert an Array like ['something', 'another thing'] to an ES6 Iterable?

An Array already is an ES6 iterable.

"Iterable" is a capability that a number of different types of objects can have and an Array has that capability built-in (as of ES6).

For example, you can directly use the iterator capabilities with something like:

for (let item of ['something', 'another thing']) {
     console.log(item);
}

Or, you could directly get the iterator with this:

const myIterator = ['something', 'another thing'].entries();
console.log(myIterator.next().value);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM