【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中提供。

相关推荐
Nturmoils4 天前
书签真正难的不是收藏,而是找回来:我是怎么做这个 Chrome 插件的
javascript·后端·浏览器
爱学习的程序媛4 天前
浏览器工作原理全景解析
前端·浏览器·web
Jack N5 天前
2026 浏览器原理 常见面试题(附答案)
前端·html·浏览器
哆哆啦005 天前
URL 重写规则和静态资源解析逻辑
前端·浏览器·url
韭菜炒大葱6 天前
讲讲 浏览器的缓存机制
前端·面试·浏览器
xiaoxue..6 天前
讲讲 浏览器的缓存机制
前端·缓存·面试·浏览器
七夜zippoe7 天前
OpenClaw Browser:浏览器控制入门
ai·自动化·浏览器·browser·openclaw
Mac的实验室11 天前
perplexity要验证手机号怎么办?2026年登陆perplexity要验证电话号码的解决办法(附验证方法)
搜索引擎·浏览器
kyriewen12 天前
我开发的 Chrome 扒图浏览器插件又更新了❗
前端·chrome·浏览器
昼猫14 天前
前端打印分页技术探讨与 PrintomJs 方案
javascript·浏览器