本文介绍了是否按升序排列的JavaScript array.forEach遍历元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在JavaScript的我可以有孔的阵列:

In javascript I can have an array with holes:

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).

(即。它看起来像阵列内部某种树木和对象是哈希表)。

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

我刚刚发现犀牛的JavaScript穿越不存在的元素也:
(不像for..in结合使用)。

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

推荐答案

借助规格及MDN's Array.forEach())页面都显示为 .forEach算法(,它肯定会遍历以升序索引顺序数组元素(跳过从未被分配了一个价值指数)。

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).

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

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

这篇关于是否按升序排列的JavaScript array.forEach遍历元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 03:43