语句创建一个循环来迭代可迭代的对象。在 ES6 中引入的
- for...of
循环,以替代
- for...of
和
- for...in
,并支持新的迭代协议。
- forEach()
允许你遍历 Arrays(数组), Strings(字符串), Maps(映射), Sets(集合)等可迭代的数据结构等。
- for...of
- for (variable of iterable) {
- statement
- }
我们来探讨一些用例。
Arrays(数组)就是类列表(list-like)对象。数组原型上有各种方法,允许对其进行操作,比如修改和遍历等操作。下面手在一个数组上进行的
操作:
- for...of
- // array-example.js
- const iterable = ['mini', 'mani', 'mo'];
- for (const value of iterable) {
- console.log(value);
- }
- // Output:
- // mini
- // mani
- // mo
其结果就是打印出
数组中的每一个值。
- iterable
Demo: https://jsbin.com/dimahag/edit?js,console
Map 对象就是保存 key-value(键值) 对。对象和原始值可以用作 key(键)或 value(值)。Map 对象根据其插入方式迭代元素。换句话说,
循环将为每次迭代返回一个 key-value(键值) 数组。
- for...of
- // map-example.js
- const iterable = new Map([['one', 1], ['two', 2]]);
- for (const [key, value] of iterable) {
- console.log(`Key: ${key} and Value: ${value}`);
- }
- // Output:
- // Key: one and Value: 1
- // Key: two and Value: 2
Demo: https://jsbin.com/lofewiw/edit?js,console
Set(集合) 对象允许你存储任何类型的唯一值,这些值可以是原始值或对象。 Set(集合) 对象只是值的集合。 Set(集合) 元素的迭代基于其插入顺序。 Set(集合) 中的值只能发生一次。如果您创建一个具有多个相同元素的 Set(集合) ,那么它仍然被认为是单个元素。
- // set-example.js
- const iterable = new Set([1, 1, 2, 2, 1]);
- for (const value of iterable) {
- console.log(value);
- }
- // Output:
- // 1
- // 2
尽管我们的 Set(集合) 有多个
和
- 1
,但输出的只有
- 2
和
- 1
。
- 2
Demo: https://jsbin.com/fajozob/edit?js,console
字符串用于以文本形式存储数据。
- // string-example.js
- const iterable = 'javascript';
- for (const value of iterable) {
- console.log(value);
- }
- // Output:
- // "j"
- // "a"
- // "v"
- // "a"
- // "s"
- // "c"
- // "r"
- // "i"
- // "p"
- // "t"
这里,对字符串执行迭代,并打印出每个索引上的字符。
Demo: https://jsbin.com/rixakeg/edit?js,console
把一个参数对象看作是一个类数组(array-like)对象,并且对应于传递给函数的参数。这是一个用例:
- // arguments-example.js
- function args() {
- for (const arg of arguments) {
- console.log(arg);
- }
- }
- args('a', 'b', 'c');
- // Output:
- // a
- // b
- // c
你可能会想,发生了什么事?! 如前所述,当调用函数时,
会接收传入
- arguments
函数的任何参数。所以,如果我们传递 20 个参数给
- args()
函数,我们将打印出 20 个参数。
- args()
Demo: https://jsbin.com/ciqabov/edit?js,console
生成器是一个函数,它可以退出函数,稍后重新进入函数。
- // generator-example.js
- function * generator() {
- yield 1;
- yield 2;
- yield 3;
- };
- for (const g of generator()) {
- console.log(g);
- }
- // Output:
- // 1
- // 2
- // 3
定义了一个生成器函数,该函数返回生成器对象(Generator object)。更多关于生成器相关的信息,可以点击这里。
- function*
Demo: https://jsbin.com/faviyi/edit?js,console
JavaScript 提供了四种已知的终止循环执行的方法:
、
- break
、
- continue
和
- return
。让我们来看一个例子:
- throw
- const iterable = ['mini', 'mani', 'mo'];
- for (const value of iterable) {
- console.log(value);
- break;
- }
- // Output:
- // mini
在这个例子中,我们使用
关键字在一次执行后终止循环,所以只有
- break
被打印出来。
- mini
Demo: https://jsbin.com/tisuken/edit?js,console
循环仅适用于迭代。 而普通对象不可迭代。 我们来看一下:
- for...of
- const obj = { fname: 'foo', lname: 'bar' };
- for (const value of obj) { // TypeError: obj[Symbol.iterator] is not a function
- console.log(value);
- }
在这里,我们定义了一个普通对象
,并且当我们尝试
- obj
对其进行操作时,会报错:
- for...of
。
- TypeError: obj[Symbol.iterator] is not a function
Demo: https://jsbin.com/sotidu/edit?js,console
我们可以通过将类数组(array-like)对象转换为数组来绕过它。该对象将具有一个
属性,其元素必须可以被索引。我们来看一个例子:
- length
- // object-example.js
- const obj = { length: 3, 0: 'foo', 1: 'bar', 2: 'baz' };
- const array = Array.from(obj);
- for (const value of array) {
- console.log(value);
- }
- // Output:
- // foo
- // bar
- // baz
方法可以让我通过类数组(array-like)或可迭代对象来创建一个新的 Array(数组) 实例。
- Array.from()
Demo: https://jsbin.com/miwofin/edit?js,console
循环将遍历对象的所有可枚举属性。
- for...in
- //for-in-example.js
- Array.prototype.newArr = () = >{};
- Array.prototype.anotherNewArr = () = >{};
- const array = ['foo', 'bar', 'baz'];
- for (const value in array) {
- console.log(value);
- }
- // Outcome:
- // 0
- // 1
- // 2
- // newArr
- // anotherNewArr
不仅枚举上面的数组声明,它还从构造函数的原型中查找继承的非枚举属性,在这个例子中,
- for...in
和
- newArr
也会打印出来。
- anotherNewArr
Demo: https://jsbin.com/quxojof/edit?js,console
更多用于特定于集合(如数组和对象),但不包括所有对象。
- for...of
注意:任何具有
属性的元素都是可迭代的。
- Symbol.iterator
- Array.prototype.newArr = () = >{};
- const array = ['foo', 'bar', 'baz'];
- for (const value of array) {
- console.log(value);
- }
- // Outcome:
- // foo
- // bar
- // baz
不考虑构造函数原型的不可枚举属性。它只需要查找可枚举属性并将其打印出来。
- for...in
Demo: https://jsbin.com/sakado/edit?js,console
了解
循环的使用可以在开发过程中节省大量的时间。 希望本文帮助你在JavaScript开发中了解和编写更好的循环结构。 让你快乐编码!
- for...of
JavaScript 中循环迭代数组
完整的示例代码
来源: http://www.css88.com/archives/8308