【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会直接返回它。如果object有Symbol.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中提供。

相关推荐
afumu1 天前
免费 API 满地都是,为什么还有人做「让网页 AI 干活」这种东西
javascript·chrome·浏览器
htzyl2061 天前
前端监控最容易写错的 18 个地方:从平均值到 beforeunload
前端·浏览器
afumu2 天前
在 Chrome 扩展里抓网页 AI 的工具调用:patch fetch 和看 DOM,我两条路都跑过
javascript·chrome·浏览器
ShineWinsu4 天前
对于Qt:QTabWidget常见属性的解析
开发语言·数据库·c++·qt·浏览器·qtabwidget·标签页
码云之上11 天前
10 颗一模一样的「Edit」:中后台 Web Agent 怎么找到对的那一行
前端·人工智能·浏览器
花开盛夏^.^15 天前
电脑打开多个浏览器Web窗口占用较大的运行内存的原因
前端·浏览器
AAA程序技术22 天前
从 DOM 到屏幕:浏览器渲染管线六步拆解
前端·浏览器
szp20051 个月前
Cache API 会丢掉 URL 里的 #:我的 9 个模型分块一直写在同一条缓存上
javascript·浏览器
SoulRoar.1 个月前
Armbian 上为虚拟桌面安装 Chromium 浏览器
浏览器·armbian·虚拟显示器