简体   繁体   English

将数组转换为 ES6 可迭代

[英]Convert Array to ES6 Iterable

I have found the answer to: Convert ES6 Iterable to Array我找到了答案: 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?如何将['something', 'another thing']类的数组转换为 ES6 Iterable?

An Array already is an ES6 iterable. Array已经是 ES6 可迭代对象。

"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). “可迭代”是许多不同类型的对象可以拥有的能力,而数组具有内置的能力(从 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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