如何摆脱数字属性的自动排序? [英] How do I get rid of automatic ordering of numeric properties?

查看:274
本文介绍了如何摆脱数字属性的自动排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个对象 newData 我在其中分配属性,如下所示:

I have an object newData where I assign properties to it like so:

newData['5'] = 'test 1';
newData['3'] = 'test 2';
newData['8'] = 'test 3';

我得到:

{
    '3': 'test 2',
    '5': 'test 1',
    '8': 'test 3'
}

但是我想维护属性的插入顺序,这样我就得到了:

But I want to maintain insertion order of properties, such that this is what I get:

{
    '5': 'test 1',
    '3': 'test 2',
    '8': 'test 3'
}

哪里5,3和8保持插入的顺序。 JavaScript对象不应该有一个命令,但它显然是这样的,而这正在严重搞砸循环。

Where 5, 3, and 8 stay in the order of insertion. JavaScript objects should not have an order, but it obviously does... and this is severely messing up the loop.

我必须有键,由于JavaScript数组不支持自定义键,这需要一个对象。如何添加到对象并保持它无序,因为它应该是?

I have to have keys and since JavaScript arrays don't support custom keys, this needs to be an object. How do I add to an object and keep it unordered as it is supposed to be?

推荐答案

对象属性 do 具有一定的顺序,与插入顺序不同。它描述在

Object properties do have a certain order, differing from insertion order. It's described in the ECMAScript 2015 Language Specification:


9.1.12 [[OwnPropertyKeys]]()

当调用 O 的[[OwnPropertyKeys]]内部方法时,将执行以下步骤:

When the [[OwnPropertyKeys]] internal method of O is called the following steps are taken:


  1. 成为新的空列表

  2. 对于每个自己的 O 这是一个整数索引,以升序数字索引顺序

    a。将 P 添加为键的最后一个元素

  3. 对于每个自己的 的国际/字符串但不是整数索引,在属性创建顺序

    a。将 P 添加为键的最后一个元素

  4. 对于每个自己的 的国际/符号,在财产创建订单

    a。将 P 添加为键的最后一个元素

  5. 返回。 $ b
  1. Let keys be a new empty List.
  2. For each own property key P of O that is an integer index, in ascending numeric index order
    a. Add P as the last element of keys.
  3. For each own property key P of O that is a String but is not an integer index, in property creation order
    a. Add P as the last element of keys.
  4. For each own property key P of O that is a Symbol, in property creation order
    a. Add P as the last element of keys.
  5. Return keys.


在上面,属性被定义为列表。然后,当遇到一个整数的属性时,它将以升序数字顺序添加到List keys ,从而导致您观察到的3,5,8。

In the above, the properties are defined as the List keys. Then, when a property that is an integer is encountered, it is added to List keys in ascending numeric order, thus leading to 3, 5, 8 you observe.

如果您希望按照插入方式遵守顺序的有序键和值对,则必须使用符合顺序的内容,例如ES6 地图 。根据文档:

If you want ordered keys and value pairs that respects order based on insertion, you must use something that respects order, such as an ES6 Map. Per the documentation:


Map对象以插入顺序迭代其元素 - a for ... of 循环为每次迭代返回一个 [key,value] 的数组。

A Map object iterates its elements in insertion order — a for...of loop returns an array of [key, value] for each iteration.

这是一个例子:

const obj = new Map([
  [5, "test 1"],
  [3, "test 2"],
  [8, "test 3"]
]);

for(let [key, value] of obj) {
  console.log(key, value);
}

首先,传递一个数组给构造函数。 Map 构造函数接受迭代,并将子数组用作键和值对。 地图基本上如下所示:

First, you pass an array of arrays to the constructor. The Map constructor takes in an iterable, and uses the subarrays as key and value pairs. The Map essentially looks like this:

+-----+----------+
| Key | Value    |
+-----+----------+
|  5  | "test 1" |
|  3  | "test 2" |
|  8  | "test 3" |
+-----+----------+

接下来,您使用来迭代 Map 。如文档中所述,迭代器返回一个 [key,value] 数组,以便您可以使用数组解构。因此, key value 保存 Map ,并记录它们提供所需的有序结果。

Next you use a for...of loop to iterate over the Map. As noted in the documentation, an array of [key, value] is returned by the iterator so you can use array destructuring. Thus, key and value hold the values of the keys and values of the Map respectively, and logging them provides the desired, ordered result.

这篇关于如何摆脱数字属性的自动排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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