“对于...”循环迭代遵循JavaScript中的数组顺序? [英] Does "for...of" loop iteration follow the array order in JavaScript?

查看:111
本文介绍了“对于...”循环迭代遵循JavaScript中的数组顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在中使用 for ...的数组迭代不能保证顺序,但是ES6引入了一个新的 / code>。

Iterating over an array using for...in doesn't guarantee order, however ES6 introduces a new construct for...of.

我对实现的有限测试表明它在数组上按顺序迭代,但这个属性有保证吗?

My limited testing of implementations of for...of indicates that it does iterate in order on array, but is this property guaranteed?

推荐答案


使用迭代数组。 ..在不保证订单,但是ES6引入了。

Iterating over an array using for...in doesn't guarantee order, however ES6 introduces a new construct for...of.

我对的实现进行了有限的测试,表明它在数组上按顺序进行迭代,但是这个属性是否有保证?

My limited testing of implementations of for...of indicates that it does iterate in order on array, but is this property guaranteed?

是的,数组上的顺序由数组迭代器定义:它将访问数组以数字索引顺序(包括不存在的数组),如稀疏数组中的那些;或者也许应该是稀疏arr中的那些 ays :-)):

Yes, the order of for-of on arrays is guaranteed by the array iterator definition: It will visit the entries in the array in numeric index order (including ones that don't exist, such as those in sparse arrays — or perhaps that should be those not in sparse arrays :-) ):

实例 Babel 的REPL,这里是使用最新浏览器的用户的现场代码段:

Live Example on Babel's REPL, and here's an on-site snippet for those using an up-to-date browser:

"use strict";
let a = [];
a[3] = 'd';
a[0] = 'a';
a.foo = "f";
for (let v of a) {
  console.log(v);
}

输出:


a
undefined
undefined
d

(两个 undefined 在Babel的REPL中显示为空白)

(The two undefineds show as blank in Babel's REPL.)

上述两件事要注意:


  1. 即使数组具有枚举属性 foo ,它不被访问。

数组是稀疏的, 访问不是现在(在索引1和2)。

The array is sparse, and for-of did visit the two entries that aren't present (at indexes 1 and 2).

for-in ,但是,有保证的订单,甚至在ES2015(也称为ES6)中。很容易阅读规范,但它并不是这样;有关详细信息,请参阅此答案。另请注意,在数组上使用 for-in 不仅将访问数组的索引,而且可以访问数组的枚举属性名称的全部包括非索引属性名称。所以例如,即使在ES2015中,这样:

for-in, however, does not have a guaranteed order, not even in ES2015 (aka "ES6"). It's really easy to read the spec such that it does, but it doesn't; see this answer for details. Also note that using for-in on arrays will not only visit the "indexes" of the arrays, but all of the array's enumerable property names, including non-index property names. So for example, even in ES2015, this:

"use strict";
var a = [];
a.foo = "f";
a[3] = 'd';
a[0] = 'a';
a.bar = "b";
var key;
for (key in a) {
  console.log(key);
}

...可能输出:


0
3
foo
bar

...或


foo
bar
0
3

...或其他,虽然很可能是当前的JavaScript实现集(但不是规范要求)所有数字键(索引)将在其他属性之前或之后被分组在一起。

...or something else, although it's very likely with the current set of JavaScript implementatoins (but not required by the spec) that the all-digits keys ("indexes") will be grouped together, either before or after the other properties.

假设在$上没有枚举属性c $ c> Array.prototype Object.prototype (默认情况下没有)。如果有的话,我们也会看到它们。

That assumes there are no enumerable properties on Array.prototype or Object.prototype (by default there aren't). If there were, we'd see them as well.

如果你想循环一个数组的 for-of 是ES2015的好工具,以及其他有用的工具,如 Array#forEach forEach 特别适用于稀疏数组;它跳过不存在的条目)。 for-in 很少是一个不错的选择。 此其他答案中有详尽的选项列表。

If you want to loop through an array's values, for-of is a great tool as of ES2015, alongside the other useful tools such as Array#forEach (forEach is particularly handy on sparse arrays; it skips the entries that don't exist). for-in is rarely a good choice. There's an exhaustive list of options in this other answer.

这篇关于“对于...”循环迭代遵循JavaScript中的数组顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆