【V8引擎blog翻译-191】迭代器助手

发布时间2024年3月27日·标记为ECMAScript

Iterator helper是Iterator prototype上的一组新方法,它们有助于迭代器的一般使用。由于这些帮助器方法位于迭代器原型上,因此任何在其原型链上具有Iterator.prototype的对象(例如数组迭代器)都将获得这些方法。在下面的小节中,我们解释迭代器助手。所有提供的示例都在包含博客文章列表的博客存档页面中工作,说明迭代器助手如何有助于查找和操纵文章。你可以在V8博客页面上试试!

.map(mapperFn)

map以mapper函数作为参数。这个帮助器返回一个迭代器的值,并将映射器函数应用于原始迭代器值。

js 复制代码
// Select the list of blog posts from a blog archive page.
const posts = document.querySelectorAll('li:not(header li)');

// Get the list of posts, return a list of their text content (titles) and log them.
for (const post of posts.values().map((x) => x.textContent)) {
  console.log(post);
}

.filter(filtererFn)

filter以filter函数作为参数。这个帮助器返回一个迭代器,它的值来自过滤器函数返回一个真实值的原始迭代器。

js 复制代码
// Select the list of blog posts from a blog archive page.
const posts = document.querySelectorAll('li:not(header li)');

// Filter blog posts that includes `V8` in their text content (titles) and log them.
for (const post of posts.values().filter((x) => x.textContent.includes('V8'))) {
  console.log(post);
} 

.take(limit)

take以一个整数作为参数。这个帮助器返回一个迭代器,从原始迭代器返回值,直到limit值。

js 复制代码
// Select the list of blog posts from a blog archive page.
const posts = document.querySelectorAll('li:not(header li)');

// Select 10 recent blog posts and log them.
for (const post of posts.values().take(10)) {
  console.log(post);
}

.drop(limit)

drop以一个整数作为参数。这个帮助器从原始迭代器返回值的迭代器,从limit值之后的值开始。

js 复制代码
// Select the list of blog posts from a blog archive page.
const posts = document.querySelectorAll('li:not(header li)');

// Drop 10 recent blog posts and log the rest of them.
for (const post of posts.values().drop(10)) {
  console.log(post);
}

.flatMap(mapperFn)

flatMap以mapper函数作为参数。这个帮助器返回一个迭代器,该迭代器的值是通过将mapper函数应用于原始迭代器值而产生的。也就是说,mapper函数返回的迭代器被展平为这个helper返回的迭代器。

js 复制代码
// Select the list of blog posts from a blog archive page.
const posts = document.querySelectorAll('li:not(header li)');

// Get list of tags of the blog posts and log them. Each post can have more than
// one tag.
for (const tag of posts.values().flatMap((x) => x.querySelectorAll('.tag').values())) {
    console.log(tag.textContent);
}

.reduce(reducer [, initialValue ])

reduce接受一个reducer函数和一个可选的初始值。这个helper返回一个值作为对迭代器的每个值应用reducer函数的结果,同时跟踪应用reducer的最后一个结果。当reducer函数处理迭代器的第一个值时,初始值被用作reducer函数的起点。

js 复制代码
// Select the list of blog posts from a blog archive page.
const posts = document.querySelectorAll('li:not(header li)');

// Get list of tags for all posts.
const tagLists = posts.values().flatMap((x) => x.querySelectorAll('.tag').values());

// Get text context for each tag in the list.
const tags = tagLists.map((x) => x.textContent);

// Counts posts with security tag.
const count = tags.reduce((sum , value) => sum + (value === 'security' ? 1 : 0), 0);
console.log(count);

.toArray()

toArray从迭代器值返回数组。

js 复制代码
// Select the list of blog posts from a blog archive page.
const posts = document.querySelectorAll('li:not(header li)');

// Create an array from the list of 10 recent blog posts.
const arr = posts.values().take(10).toArray();

.forEach(fn)

forEach接受一个函数作为参数,并应用于迭代器的每个元素。这个辅助器被调用它的副作用,并返回undefined

js 复制代码
// Select the list of blog posts from a blog archive page.
const posts = document.querySelectorAll('li:not(header li)');

// Get the dates that at least one blog post is published and log them.
const dates = new Set();
const forEach = posts.values().forEach((x) => dates.add(x.querySelector('time')));
console.log(dates);

.some(fn)

some将谓词函数作为参数。如果任何迭代器元素在应用该函数时返回true,则该助手返回true。迭代器在调用some后被消耗。

javascript 复制代码
// Select the list of blog posts from a blog archive page.
const posts = document.querySelectorAll('li:not(header li)');

// Find out if text content (title) of any blog post includes the `Iterators`
// keyword.
posts.values().some((x) => x.textContent.includes('Iterators'));

.every(fn)

every将谓词函数作为参数。如果每个迭代器元素在应用该函数时都返回true,则该助手返回true。迭代器在调用every后被消耗。

js 复制代码
// Select the list of blog posts from a blog archive page.
const posts = document.querySelectorAll('li:not(header li)');

// Find out if text content (title) of all blog post includes the `V8` keyword.
posts.values().every((x) => x.textContent.includes('V8'));

.find(fn)

find将谓词函数作为参数。这个帮助器返回迭代器的第一个值,如果函数返回一个truthy值,或者如果迭代器没有值返回,则返回undefined

js 复制代码
// Select the list of blog posts from a blog archive page.
const posts = document.querySelectorAll('li:not(header li)');

// Log the text content (title) of the recent blog post includes `V8` keyword.
console.log(posts.values().find((x) => x.textContent.includes('V8')).textContent);

Iterator.from(object)

from是一个静态方法,它接受一个对象作为参数。如果object已经是Iterator的实例,helper会直接返回它。如果objectSymbol.iterator,这意味着它是一个可迭代的,它的Symbol.iterator方法被调用以获得迭代器,帮助器返回它。否则,创建一个新的Iterator对象(继承自Iterator.prototype并具有next()return()方法),它包装了object并由这个帮助器返回。

js 复制代码
// Select the list of blog posts from a blog archive page.
const posts = document.querySelectorAll('li:not(header li)');

// First create an iterator from the posts. Then, log the text content (title) of 
// the recent blog post that includes the `V8` keyword.
console.log(Iterator.from(posts).find((x) => x.textContent.includes('V8')).textContent);

可用性

迭代器助手在V8 v12.2中提供。

相关推荐
打小就很皮...10 天前
浏览器存储 Cookie,Local Storage和Session Storage
前端·缓存·浏览器
小妖66612 天前
chrome 浏览器怎么不自动提示是否翻译网站
浏览器
大名人儿16 天前
【浏览器网络请求全过程】
浏览器·网络请求·详解·全过程
windliang18 天前
Cursor 写一个网页标题重命名的浏览器插件
前端·浏览器
前端付豪18 天前
1、为什么浏览器要有渲染流程? ——带你一口气吃透 Critical Rendering Path
前端·后端·浏览器
啵啵学习18 天前
浏览器插件,提示:此扩展程序未遵循 Chrome 扩展程序的最佳实践,因此已无法再使用
前端·chrome·浏览器·插件·破解
前端南玖19 天前
通过performance面板验证浏览器资源加载与渲染机制
前端·面试·浏览器
mx95122 天前
真实业务场景:在React中使用Web Worker实现HTML导出PDF的性能优化实践
性能优化·浏览器
前端南玖25 天前
浏览器如何确定最终的CSS属性值?解析计算优先级与规则
前端·css·浏览器
NSJim25 天前
微软Edge浏览器字体设置
edge·浏览器·字体设置