gpt4 book ai didi

javascript - 我可以假定 Object.keys 的响应顺序相同吗?

转载 作者:行者123 更新时间:2023-11-30 17:36:40 25 4
gpt4 key购买 nike

{"/book":1,"/order":2,"deliver":3}

在我的应用程序的 UI 中,当我点击/book 时,我会从 map 上知道要去哪一步。 有时我只想通过递增数字来进入下一步,但要确保 URL 也发生变化。

如何进行从步骤到键的反向映射。

我遇到了 ECMAScript 5 中引入的 Object.keys。返回的键列表中元素的顺序是否始终相同?

["/book","/order","deliver"] 

如果是,那为什么会这样?字典是无序的吧?

最佳答案

ECMAScript 5.1 specification指出

When the keys function is called with argument O, the following steps are taken:

  1. If the Type(O) is not Object, throw a TypeError exception.
  2. Let n be the number of own enumerable properties of O
  3. Let array be the result of creating a new Object as if by the expression new Array(n) where Array is the standard built-in constructor with that name.
  4. Let index be 0.
  5. For each own enumerable property of O whose name String is P: (a) Call the [[DefineOwnProperty]] internal method of array with arguments ToString(index), the PropertyDescriptor {[[Value]]: P, [[Writable]]: true, [[Enumerable]]: true, [[Configurable]]: true}, and false. (b) Increment index by 1.
  6. Return array.

If an implementation defines a specific order of enumeration for the for-in statement, that same enumeration order must be used in step 5 of this algorithm.

Mozilla 开发网络对 for-in loop 有这样的说法:

A for...in loop iterates over the properties of an object in an arbitrary order (see the delete operator for more on why one cannot depend on the seeming orderliness of iteration, at least in a cross-browser setting).

以及对 delete operator 的引用导致这个:

Although ECMAScript makes iteration order of objects implementation-dependent, it may appear that all major browsers support an iteration order based on the earliest added property coming first (at least for properties not on the prototype). However, in the case of Internet Explorer, when one uses delete on a property, some confusing behavior results, preventing other browsers from using simple objects like object literals as ordered associative arrays. In Explorer, while the property value is indeed set to undefined, if one later adds back a property with the same name, the property will be iterated in its old position--not at the end of the iteration sequence as one might expect after having deleted the property and then added it back.

So if you want to simulate an ordered associative array in a cross-browser environment, you are forced to either use two separate arrays (one for the keys and the other for the values), or build an array of single-property objects, etc.

我认为由此得出的结论是,在单个浏览器中,顺序将是任意但一致的,但如果您比较多个浏览器,那么这些浏览器之间的顺序可能会有所不同(尤其是在您删除并重新添加时属性)。

编辑:

如果你想根据关联的值对键进行排序,那么你可以这样做:

var map = { b: 2, a: 1, c: 3 };

var keys = Object.keys( map );

console.log( keys ); // [ 'b', 'a', 'c' ]

var sorted_keys = keys.slice(0); // sliced so that we can see the difference in order

sorted_keys.sort( function( a, b ){ return map[a] - map[b]; } );

console.log( sorted_keys ); // [ 'a', 'b', 'c' ]

关于javascript - 我可以假定 Object.keys 的响应顺序相同吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21902874/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com