for.in& ndash的迭代顺序;不是通过插入(更多?) [英] Iteration order of for.in – not by insertion (any more?)

查看:77
本文介绍了for.in& ndash的迭代顺序;不是通过插入(更多?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据我的研究, for..in 循环中键的顺序应该是未定义/不可靠的 - 但是,如果不受干扰,应该按插入顺序 - 但它不是:

According to my research, the order of keys in a for..in loop should be undefined/unreliable – but, if left undisturbed, should be in insertion order – but it's not:

我从数据库中获取此数据对象,按名称排序:

I fetch this data object from the database, ordered by name:

var travel = {  
    '2': { name: 'bus',  price: 10 },  
    '3': { name: 'foot', price: 0 },  
    '1': { name: 'taxi', price: 100 }  
}  
for (way in travel) console.log( travel[way].name ) // => taxi, bus, foot  

按键以数字方式订购(在所有Chrome,Firefox和Edge中) 。为什么?

The keys get ordered numerically (in all of Chrome, Firefox, and Edge). Why?

并且(因为我错了)如何通过 .name 命令迭代它们?

And (since I was wrong) how can I iterate through them ordered by .name?

推荐答案


根据我的研究,for..in循环中键的顺序应该是未定义的/不可靠

According to my research, the order of keys in a for..in loop should be undefined/unreliable

未定义,是。



  • 但是,如果不受干扰,应按插入顺序

否,你是第一次是对的:它是未定义的。即使在ES2015(又名ES6)及以上,它确实为其他一些操作提供了属性订单,旧的操作 for-in 对象。密钥不需要遵循为新密钥定义的订单。

No, you were right the first time: It's undefined. Even in ES2015 (aka "ES6") and above, which do provide property order for some other operations, the older operations for-in and Object.keys are not required to follow the order defined for the new ones.

在其他操作中( Object.getOwnPropertyNames JSON.serialize ,...),订单(此处定义)不是纯粹的插入顺序:名称为数组索引的属性根据规范的定义*首先按数字顺序排列。大多数主要的JavaScript引擎已经更新了对 for-in 的处理,以匹配他们对这些新操作的处理(许多已经对数组索引进行了不同的处理,但它们是否有所不同)把那些放在非数组索引之前或之后),但同样,它是未定义的,你不应该依赖它。

In those other operations (Object.getOwnPropertyNames, JSON.serialize, ...), the order (defined here) isn't purely insertion order: Properties whose names are array indexes according to the spec's definition* come first, in numeric order. Most major JavaScript engines have updated their handling of for-in to match their handling of these new operations (many already did treat array indexes differently, but they varied in terms of whether they put those before the non-array-indexes or after), but again, it's undefined, and you shouldn't rely on it.

如果你想要纯插入顺序, ES2015的 Map 提供了,无论密钥的值如何。对象没有。

If you want pure insertion order, ES2015's Map provides that, regardless of the value of the key. Objects don't.

这是一个使用 Map 的例子:

const map = new Map([
  ['2', { name: 'bus',  price: 10 }],
  ['3', { name: 'foot', price: 0 }],
  ['1', { name: 'taxi', price: 100 }]
]);
for (const entry of map.values()) { // bus, foot, taxi
  console.log(entry.name);
}

* 数组索引的规范定义是:

* The spec's definition of an "array index" is:


整数索引是一个字符串值属性键,它是一个规范数字字符串(见7.1.16),其数值为+0或正整数≤2 53-1 数组索引是整数索引,其数值 i 在+0≤i<1的范围内。 2 32-1

An integer index is a String-valued property key that is a canonical numeric String (see 7.1.16) and whose numeric value is either +0 or a positive integer ≤ 253-1. An array index is an integer index whose numeric value i is in the range +0 ≤ i < 232-1.

这篇关于for.in&amp; ndash的迭代顺序;不是通过插入(更多?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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