javascript array.forEach是否按升序遍历元素

时间:2022-03-28 07:37:56

In javascript I can have an array with holes:

在javascript中我可以有一个带孔的数组:

a = [];
a[0] = 100;
a[5] = 200;
a[3] = 300;

a.forEach(function(x) {alert(x);});

I could not find information about whether elements would be processed in ascending order or this is not reliable fact.

我找不到有关元素是否按升序处理的信息,或者这不是可靠的事实。

I checked that "for .. in" loop traverses array indices in ascending order, while property names of an object are traversed in the same order they were added to object (at least it looks so).

我检查了“for ... in”循环以升序遍历数组索引,而对象的属性名称以与添加到对象相同的顺序遍历(至少看起来如此)。

(I.e. it looks like arrays are internally trees of some kind and objects are hashtables.)

(即,它看起来像数组是某种类型的内部树,而对象是哈希表。)

I just found that Rhino JavaScript traverses non-existent elements also: http://ideone.com/7Z3AFh (unlike for..in).

我刚刚发现Rhino JavaScript也遍历了不存在的元素:http://ideone.com/7Z3AFh(与for..in不同)。

3 个解决方案

#1


30  

The ECMA-262, 5th edition specification and MDN's Array.forEach() page both show the algorithm for .forEach(), and it will definitely iterate over array elements in ascending index order (skipping indices that were never assigned a value).

ECMA-262,第5版规范和MDN的Array.forEach()页面都显示.forEach()的算法,它肯定会按升序索引顺序迭代数组元素(跳过从未赋值的索引)。

Of course, some browsers may not implement that algorithm properly, but I'm not aware of any that don't.

当然,有些浏览器可能无法正确实现该算法,但我不知道有哪些不能。

#2


13  

The specification says forEach will visit the array elements in numeric order. It doesn't visit elements that don't exist. See the link for details. So for your example array, it will visit element 0, then 3, then 5. The order in which you add them to the array has no effect on the order in which they're visited.

规范说forEach将按数字顺序访问数组元素。它不访问不存在的元素。请参阅链接了解详细信息。因此,对于您的示例数组,它将访问元素0,然后是3,然后是5.将它们添加到数组的顺序对它们的访问顺序没有影响。

I checked that "for .. in" loop traverses array indices in ascending order, while property names of an object are traversed in the same order they were added to object (at least it looks so).

我检查了“for ... in”循环以升序遍历数组索引,而对象的属性名称以与添加到对象相同的顺序遍历(至少看起来如此)。

The order in which for-in visits object properties is not defined by the specification, not even in ES2015 (aka ES6), despite the fact that ES2015 defines an order for object properties — that order doesn't apply to for-in or Object.keys. (More about that in this answer.) If you want to visit properties in the order defined in ES2015, you can use Object.getOwnPropertyNames (for properties that aren't defined with Symbol names) or Reflect.ownKeys (for both Symbol and string property names [remember numeric property names are really strings]). Both of those do respect property order.

尽管ES2015定义了对象属性的顺序,但该顺序不适用于for-in或Object,因此规范未定义for-in访问对象属性的顺序,即使在ES2015(aka ES6)中也是如此.keys。 (有关此答案的详细信息。)如果要按ES2015中定义的顺序访问属性,可以使用Object.getOwnPropertyNames(对于未使用Symbol名称定义的属性)或Reflect.ownKeys(对于Symbol和string)属性名称[记住数字属性名称实际上是字符串])。这两个都尊重财产秩序。

#3


3  

Straight out of the ECMAScript standard

直接脱离ECMAScript标准

forEach calls callbackfn once for each element present in the array, in ascending order. callbackfn is called only for elements of the array which actually exist; it is not called for missing elements of the array.

forEach按升序为数组中的每个元素调用一次callbackfn。 callbackfn仅为实际存在的数组元素调用;它不会被调用缺少数组的元素。

So Array.forEach will skip certain elements in an array. Your example

因此,Array.forEach将跳过数组中的某些元素。你的榜样

a.forEach( function( value ) { console.log( value ) }); // prints 100, 300, 200

If you do want to traverse the array in ascending order and all your elements are numbers then you can sort the array beforehand like so

如果您确实想要按升序遍历数组并且所有元素都是数字,那么您可以预先对数组进行排序

a.sort( function( a, b ) { return a - b });
// this now prints 100, 200, 300
a.forEach( function( value ) { console.log( value ) }); 

#1


30  

The ECMA-262, 5th edition specification and MDN's Array.forEach() page both show the algorithm for .forEach(), and it will definitely iterate over array elements in ascending index order (skipping indices that were never assigned a value).

ECMA-262,第5版规范和MDN的Array.forEach()页面都显示.forEach()的算法,它肯定会按升序索引顺序迭代数组元素(跳过从未赋值的索引)。

Of course, some browsers may not implement that algorithm properly, but I'm not aware of any that don't.

当然,有些浏览器可能无法正确实现该算法,但我不知道有哪些不能。

#2


13  

The specification says forEach will visit the array elements in numeric order. It doesn't visit elements that don't exist. See the link for details. So for your example array, it will visit element 0, then 3, then 5. The order in which you add them to the array has no effect on the order in which they're visited.

规范说forEach将按数字顺序访问数组元素。它不访问不存在的元素。请参阅链接了解详细信息。因此,对于您的示例数组,它将访问元素0,然后是3,然后是5.将它们添加到数组的顺序对它们的访问顺序没有影响。

I checked that "for .. in" loop traverses array indices in ascending order, while property names of an object are traversed in the same order they were added to object (at least it looks so).

我检查了“for ... in”循环以升序遍历数组索引,而对象的属性名称以与添加到对象相同的顺序遍历(至少看起来如此)。

The order in which for-in visits object properties is not defined by the specification, not even in ES2015 (aka ES6), despite the fact that ES2015 defines an order for object properties — that order doesn't apply to for-in or Object.keys. (More about that in this answer.) If you want to visit properties in the order defined in ES2015, you can use Object.getOwnPropertyNames (for properties that aren't defined with Symbol names) or Reflect.ownKeys (for both Symbol and string property names [remember numeric property names are really strings]). Both of those do respect property order.

尽管ES2015定义了对象属性的顺序,但该顺序不适用于for-in或Object,因此规范未定义for-in访问对象属性的顺序,即使在ES2015(aka ES6)中也是如此.keys。 (有关此答案的详细信息。)如果要按ES2015中定义的顺序访问属性,可以使用Object.getOwnPropertyNames(对于未使用Symbol名称定义的属性)或Reflect.ownKeys(对于Symbol和string)属性名称[记住数字属性名称实际上是字符串])。这两个都尊重财产秩序。

#3


3  

Straight out of the ECMAScript standard

直接脱离ECMAScript标准

forEach calls callbackfn once for each element present in the array, in ascending order. callbackfn is called only for elements of the array which actually exist; it is not called for missing elements of the array.

forEach按升序为数组中的每个元素调用一次callbackfn。 callbackfn仅为实际存在的数组元素调用;它不会被调用缺少数组的元素。

So Array.forEach will skip certain elements in an array. Your example

因此,Array.forEach将跳过数组中的某些元素。你的榜样

a.forEach( function( value ) { console.log( value ) }); // prints 100, 300, 200

If you do want to traverse the array in ascending order and all your elements are numbers then you can sort the array beforehand like so

如果您确实想要按升序遍历数组并且所有元素都是数字,那么您可以预先对数组进行排序

a.sort( function( a, b ) { return a - b });
// this now prints 100, 200, 300
a.forEach( function( value ) { console.log( value ) });