[Javascript] Yield an Array, String, or Any Iterable from a Generator (yield*)

Posted Answer1215

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Javascript] Yield an Array, String, or Any Iterable from a Generator (yield*)相关的知识,希望对你有一定的参考价值。

Generators allow you to use the yield * syntax to yield each iteration of nested iterable as part of the main iterations. This enables you to combine multiple arrays, strings, or any iterable with anything you want to yield from your main generator.

 

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

const reverseIterator = function* (array) {
    yield* array
    yield* array.map(letter => letter.toLowerCase())
    yield Math.random()
    yield* "wan"
}

const iterator = reverseIterator(abcs)

for (let value of iterator) {
    console.log(value)
}


/*
A
B
C
a
b
c
0.1234
w
a
n
*/

 

以上是关于[Javascript] Yield an Array, String, or Any Iterable from a Generator (yield*)的主要内容,如果未能解决你的问题,请参考以下文章

UnorderedObjectListWarning: Pagination may yield inconsistent results with an unordered object_list:

ES6mapreducefiltersort箭头函数class继承yield

js进阶四(mapreducefiltersort箭头函数class继承yield)

yield用法

Javascript生成器函数yield未在变量中赋值

[JavaScript]ECMA-6 yield语法