javascript数组迭代器与传统遍历方式的核心区别在于惰性求值与显式控制,传统方式如for循环和foreach会立即遍历所有元素,而values()返回的迭代器通过next()按需返回值,节省资源;2. 除了values(),还可使用keys()获取索引迭代器,entries()获取索引-值对迭代器,三者共同提供多维度遍历能力;3. 在处理大数据集、需手动控制流程、传递可迭代对象或结合生成器时,优先使用values()迭代器,因其支持惰性加载和细粒度操作,提升性能与灵活性。

在 JavaScript 中,你可以直接使用数组实例上的
values()
Array Iterator
for...of
next()
获取 JavaScript 数组元素的迭代器,核心就是利用
Array.prototype.values()
Array Iterator
举个例子,假设我们有一个数字数组:
const numbers = [10, 20, 30, 40];
const valueIterator = numbers.values();
console.log(valueIterator.next()); // { value: 10, done: false }
console.log(valueIterator.next()); // { value: 20, done: false }
console.log(valueIterator.next()); // { value: 30, done: false }
console.log(valueIterator.next()); // { value: 40, done: false }
console.log(valueIterator.next()); // { value: undefined, done: true }当然,在实际开发中,我们很少会这样手动调用
next()
for...of
next()
done
const fruits = ['apple', 'banana', 'cherry'];
const fruitIterator = fruits.values();
for (const fruit of fruitIterator) {
console.log(fruit);
}
// 输出:
// apple
// banana
// cherry值得注意的是,
for...of
Symbol.iterator
values()
for...of
values()
const colors = ['red', 'green', 'blue'];
for (const color of colors) { // 内部其实也是在利用 values() 提供的迭代器
console.log(color);
}在我看来,理解
values()
这真是一个好问题,因为很多人可能会疑惑,既然
for...of
values()
传统的数组遍历方式,比如
for
for (let i = 0; i < arr.length; i++)
forEach
for
forEach
而迭代器,特别是通过
values()
next()
考虑一个场景:你有一个非常庞大的数据集,可能是一个巨大的数组,或者是一个通过某种方式分块加载的数据。如果你用
forEach
values()
此外,迭代器提供了一种统一的遍历协议。不仅仅是数组,
Map
Set
Symbol.iterator
for...of
values()
是的,除了
values()
Array
Array.prototype.keys()
keys()
const students = ['Alice', 'Bob', 'Charlie'];
const indexIterator = students.keys();
for (const index of indexIterator) {
console.log(`Index: ${index}`);
}
// 输出:
// Index: 0
// Index: 1
// Index: 2在我看来,
keys()
Array.prototype.entries()
[index, value]
const products = ['Laptop', 'Mouse', 'Keyboard'];
const entryIterator = products.entries();
for (const [index, product] of entryIterator) {
console.log(`Product at index ${index}: ${product}`);
}
// 输出:
// Product at index 0: Laptop
// Product at index 1: Mouse
// Product at index 2: Keyboardentries()
[index, value]
forEach
entries()
这三个方法共同提供了对数组内容不同维度的迭代访问,覆盖了我们日常开发中绝大多数的遍历需求。
values()
在日常开发中,直接使用
for...of
values()
values()
处理非常大的数据集或潜在无限数据流: 这是迭代器最显著的优势之一。如果你的数组非常庞大,或者你正在处理一个来自网络流、文件读取等按需生成的数据序列,使用
values()
需要手动控制遍历流程时: 有时候,你可能不希望简单地从头到尾遍历整个数组。你可能需要在某个条件满足时暂停遍历,或者跳过一些元素,或者在外部循环中驱动内部的遍历。在这种情况下,直接获取
values()
next()
const tasks = ['prepare data', 'process data', 'analyze results', 'generate report'];
const taskIterator = tasks.values();
let currentTask = taskIterator.next();
while (!currentTask.done && currentTask.value !== 'analyze results') {
console.log(`Executing: ${currentTask.value}`);
currentTask = taskIterator.next();
}
console.log(`Reached critical step: ${currentTask.value}`);
// 输出:
// Executing: prepare data
// Executing: process data
// Reached critical step: analyze results这种显式的控制在构建一些复杂的流程控制逻辑时非常有用。
将数组作为通用可迭代对象传递给其他函数时: 如果你有一个函数,它被设计为接收任何可迭代对象(而不仅仅是数组),那么传递一个通过
values()
与其他迭代器协议或生成器函数结合使用时: 当你开始涉足更高级的迭代器模式,比如将多个迭代器链式连接,或者使用生成器函数来创建自定义迭代逻辑时,
values()
总而言之,虽然
for...of
values()
以上就是js 如何用values获取数组元素的迭代器的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号