如果对象有数字键,有什么现代方法可以在 Object.assign 中禁用排序?
Any modern way to disable sort in Object.assign if objects has numeric keys?
例如: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"}
有没有办法禁用排序?
不,因为(如您所说)数字键(或使用规范的术语,整数索引*)。
Object.assign
works in the order defined by [[OwnPropertyKeys]]
, which for ordinary objects is the OrdinaryOwnPropertyKeys
抽象操作,它按定义的顺序列出属性(首先是整数索引,然后是按创建顺序排列的其他字符串命名属性,然后是按创建顺序排列的符号命名属性)。结果对象的属性将以相同的方式枚举(通过遵循定义顺序的操作),因此,整数索引在数字上,然后是创建顺序中的其他属性。
如果您的键不是整数索引,您可以通过按照您想要的顺序预先创建属性来控制结果的顺序,但在整数索引键的情况下则不然。
例如,如果您的键是 a
、b
、c
和 d
,您可以确定结果对象属性的列出顺序(对于遵循顺序的操作):
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));
请注意,我在那里使用 JSON.stringify
作为输出,因为 JSON.stringify
被定义为遵循 属性 顺序(而 for-in
和 Object.keys
是不是)。
但不是你的键,它们是整数索引。
如果顺序很重要,通常对象是错误的数据结构;相反,您需要一个数组。数组也是对象,数组的某些有序性只是约定俗成(除非优化),但它们有几个重要的特定于顺序的特性,尤其是 length
属性 及其各种方法按照定义的顺序工作。
* "integer index" 定义为 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 ≤ 253-1.
例如: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"}
有没有办法禁用排序?
不,因为(如您所说)数字键(或使用规范的术语,整数索引*)。
Object.assign
works in the order defined by [[OwnPropertyKeys]]
, which for ordinary objects is the OrdinaryOwnPropertyKeys
抽象操作,它按定义的顺序列出属性(首先是整数索引,然后是按创建顺序排列的其他字符串命名属性,然后是按创建顺序排列的符号命名属性)。结果对象的属性将以相同的方式枚举(通过遵循定义顺序的操作),因此,整数索引在数字上,然后是创建顺序中的其他属性。
如果您的键不是整数索引,您可以通过按照您想要的顺序预先创建属性来控制结果的顺序,但在整数索引键的情况下则不然。
例如,如果您的键是 a
、b
、c
和 d
,您可以确定结果对象属性的列出顺序(对于遵循顺序的操作):
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));
请注意,我在那里使用 JSON.stringify
作为输出,因为 JSON.stringify
被定义为遵循 属性 顺序(而 for-in
和 Object.keys
是不是)。
但不是你的键,它们是整数索引。
如果顺序很重要,通常对象是错误的数据结构;相反,您需要一个数组。数组也是对象,数组的某些有序性只是约定俗成(除非优化),但它们有几个重要的特定于顺序的特性,尤其是 length
属性 及其各种方法按照定义的顺序工作。
* "integer index" 定义为 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 ≤ 253-1.