[Javascript] Avoid Nested For Loops with Generators
JavaScript for with nested avoid
2023-09-14 09:00:48 时间
Generators allow you to hook together multiple generators with the yield*
syntax. This allows you to branch off into many different types of iterations within the main iteration and covers complex scenarios where you usually end up reaching for nested for loops.
const abcs = ["A", "B", "C"] const shoutIterator = function* (word: string) { yield word + "!" yield word + "!!" yield word + "!!!" } const reverseIterator = function* (array: string[]) { let reversed = array.reverse(); yield* shoutIterator(array[0]); yield* shoutIterator(array[1]); yield* shoutIterator(array[2]); } const iterator = reverseIterator(abcs) for (let value of iterator) { console.log(value) }
/* C! C!! C!!! B! B!! B!!! A! A!! A!!! */
相关文章
- javascript 动态统计文本域输入字符个数
- JavaScript 错误监控Fundebug
- [Javascript] Create a Custom Iterator for Any Array
- [Cypress] install, configure, and script Cypress for JavaScript web applications -- part2
- [Javascript] Iterate Over Items with JavaScript's for-of Loop
- [Javascript] Case insensitive sorting for string arrays
- JavaScript 消息框,警告框,确认框,提示框
- [Javascript] Wait for the Fastest JavaScript Promise to Be Fulfilled with Promise.any()
- [Javascript] Hide Properties from Showing Up in "for ... in" Loops in JavaScript
- [Cypress] install, configure, and script Cypress for JavaScript web applications -- part3
- [WASM] Set up wasm-bindgen for easy Rust/JavaScript Interoperability
- [Javascript] Hositing
- javascript:null和undefined的区别(chrome 104.0.5112.101)
- arcgis for javascript 添加featurelayer,设置地图最大最小等级
- 一文搞懂JavaScript中的typeof用法
- SAP Cloud SDK for JavaScript 的搭建和使用方法介绍
- SAP Cloud SDK for JavaScript 概述
- DataGridXL 2.0 for JavaScript Crack