我应该如何以索引顺序迭代稀疏数组? [英] How should I iterate over a sparse array in index order?

查看:97
本文介绍了我应该如何以索引顺序迭代稀疏数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个稀疏数组,其内容不能保证以索引顺序插入,但需要按索引顺序迭代。要遍历稀疏数组,我了解您需要使用for..in语句。

I have a sparse array whose contents aren't guaranteed to be inserted in index order but need to be iterated through in index order. To iterate through a sparse array I understand that you need to use a for..in statement.

但是,根据这篇文章


无法保证...将以任何特定顺序返回索引

There is no guarantee that for...in will return the indexes in any particular order

像这样的stackoverflow问题表明虽然不保证对象属性订单,但是数组订单是:

But stackoverflow questions like this suggest that whilst object property orders are not guaranteed, array orders are:


在JavaScript中不保证对象中的属性顺序,您需要使用数组。

properties order in objects are not guaranted in JavaScript, you need to use an Array.

I 在Chrome,Firefox和IE的最新版本中对此进行了测试

<ol id="items"></ol>



var list = [];

function addItem(index) {
    list[index] = { idx : index };
}

var insertOrder = [ 8, 1, 9, 2, 10, 3, 11, 4, 12, 5, 13, 6, 14, 7, 15 ];

for ( var i = 0; i < 15; i++ ) {
    addItem(insertOrder[i]);
}

for(var item in list) {
    $("#items").append("<li>" + list[item].idx + "</li>");
}

所有似乎都遵守索引订单所以我可以相信这总是案件?否则,我如何以索引顺序最好地获取它们?

All appear to honor the index order so can I trust this always to be the case? Otherwise, how do I best get them in index order?

推荐答案

MDN 可以回答您的原始问题:

MDN has the answer to your original question:


注意:for..in不应该用于迭代索引顺序很重要的数组。

数组索引只是具有整数名称的可枚举属性,在其他方面与一般对象属性相同。无法保证for ... in将以任何特定顺序返回索引,并且它将返回所有可枚举属性,包括那些具有非整数名称和继承的属性。

Array indexes are just enumerable properties with integer names and are otherwise identical to general Object properties. There is no guarantee that for...in will return the indexes in any particular order and it will return all enumerable properties, including those with non–integer names and those that are inherited.

您不必使用 for..in 来遍历稀疏数组,您应该完全避免这样做

You don't have to use for..in to iterate through a sparse array, and you should definitely avoid doing so if you can.

你可以使用 .forEach

list.forEach(function (el) {  // add a second parameter if you need the indices
    $("#items").append($("<li>").text(el.idx));
});

forEach 已定义以按索引顺序进行迭代,并且仅包含存在于中的元素数组:

forEach is defined to iterate in index order and only include elements that are present in the array:


forEach 为每个元素执行一次提供的回调数组按升序排列。对于已删除或省略的索引,不会调用它。但是,对于存在且元素未定义的元素执行它。

forEach executes the provided callback once for each element present in the array in ascending order. It is not invoked for indexes that have been deleted or elided. However, it is executed for elements that are present and have the value undefined.


如果您的目标是不支持 forEach 的环境,您可以使用以下内容或该MDN页面上提供的垫片:

If you're targeting environments that don't support forEach, you can use the following, or the shim provided on that MDN page:

for (var i = 0; i < list.length; i += 1) {
    if (i in list) {
        $("#items").append($("<li>").text(list[i].idx));
    }
}

这篇关于我应该如何以索引顺序迭代稀疏数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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