zl程序教程

您现在的位置是:首页 >  前端

当前栏目

[Javascript] Creating an Iterator from an Array

JavaScript from Array an Iterator creating
2023-09-14 09:00:48 时间

Every Array has a function which you can use to create an iterator. This function can only be accessed by using the Symbol.iterator as a key on the Array. Once you have created your iterator, you can use it to iterate through each value of the Array using .next or a for loop.

 

const abcs = ["A", "B", "C"]

const createIterator = abcs[Symbol.iterator].bind(abcs)

const iterator = createIterator()

for (const i of iterator) {
    console.log(i)
}

for (const i of createIterator()) {
  console.log(i)
}