简体   繁体   English

有什么现代方法可以在Object.assign中禁用排序(如果对象具有数字键)?

[英]Any modern way to disable sort in Object.assign if objects has numeric keys?

For example: snippet img 例如: snippet img

var a = {1: '1', 2: '2'}
var b = {3: '3', 4: '4'}

Object.assign({}, a, b)
> {1: "1", 2: "2", 3: "3", 4: "4"}

Object.assign({}, b, a)
> {1: "1", 2: "2", 3: "3", 4: "4"}

Is there way to disable sorting? 有没有办法禁用排序?

No, because (as you said) of the numeric keys (or to use the spec's term, integer indexes *). 不,因为(如您所说)数字键(或使用规范的术语, 整数索引 *)。

Object.assign works in the order defined by [[OwnPropertyKeys]] , which for ordinary objects is the OrdinaryOwnPropertyKeys abstract operation, which lists the properties in a defined order (integer indexes first, then other string-named properties in order of creation, then Symbol-named properties in order of creation). Object.assign[[OwnPropertyKeys]]定义的顺序工作,对于普通对象,此对象是OrdinaryOwnPropertyKeys抽象操作,该操作按定义的顺序列出属性[[OwnPropertyKeys]]整数索引,然后按创建顺序列出其他以字符串命名的属性,然后按创建顺序按创建顺序以符号命名的属性)。 The resulting object's properties will be enumerated (by operations that follow the defined order) in the same way, and so, integer indexes, numerically, followed by other properties in creationg order. 结果对象的属性将以相同的方式枚举(通过遵循定义顺序的操作),因此,整数索引在数值上跟随,其后是其他属性(按创建顺序)。

If your keys weren't integer indexes, you could control the order of the result by pre-creating the properties in the order you wanted them, but not in the case of integer index keys. 如果您的键不是整数索引,则可以通过按所需顺序创建属性来控制结果的顺序,但对于整数索引键则不是这样。

So for instance, if your keys were a , b , c , and d , you could determine the order the resulting object's properties were listed (for operations that follow the order): 因此,例如,如果您的键是abcd ,则可以确定列出结果对象的属性的顺序(对于遵循该顺序的操作):

 const x = {a: 'a', b: 'b'}; const y = {c: 'c', d: 'd'}; const result1 = Object.assign({c: null, d: null, a: null, b: null}, x, y); console.log(JSON.stringify(result1)); const result2 = Object.assign({c: null, d: null, a: null, b: null}, y, x); console.log(JSON.stringify(result2)); const result3 = Object.assign({a: null, b: null, c: null, d: null}, x, y); console.log(JSON.stringify(result3)); const result4 = Object.assign({a: null, b: null, c: null, d: null}, y, x); console.log(JSON.stringify(result4)); 

Note that I'm using JSON.stringify there for output, because JSON.stringify is defined to follow property order (whereas for-in and Object.keys are not). 请注意,我在JSON.stringify使用JSON.stringify进行输出,因为JSON.stringify定义为遵循属性顺序(而for-inObject.keys则不是)。

But not for your keys, which are integer indexes. 但不适用于您的键,键是整数索引。

If order is important, usually an object is the wrong data structure; 如果顺序很重要,那么对象通常是错误的数据结构; instead, you'd want an array. 相反,您需要一个数组。 Arrays are also objects and some of the ordered-ness of an array is just convention (barring optimization), but they have several important order-specific features, not least the length property and their various methods that work in a defined order. 数组也是对象,数组的某些有序性只是约定(禁止优化),但它们具有一些重要的特定于订单的功能,不仅是length属性及其以定义的顺序工作的各种方法。


* "integer index" is defined here : *“整数索引” 在此处定义:

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 ≤ 2 53 -1. 整数索引是一个字符串值的属性键,它是规范的数字字符串(请参见7.1.16),其数值为+0或正整数≤2 53 -1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM