admin管理员组

文章数量:1406951

For example: 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?

For example: 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?

Share Improve this question asked Dec 19, 2017 at 7:33 TwistTwist 2751 gold badge4 silver badges12 bronze badges 5
  • 1 You should consider objects to be not sorted. Of you rely on the order of things, use arrays. – Ingo Bürk Commented Dec 19, 2017 at 7:43
  • Object properties don't have an order, so what do you mean by "sorting"? – Bergi Commented Dec 19, 2017 at 7:47
  • @Bergi: You know that's not true anymore. :-) (In real-world use, even the operations that aren't required to follow the order do so on all major JavaScript engines.) – T.J. Crowder Commented Dec 19, 2017 at 7:55
  • @T.J.Crowder On all JS engines but not all JS transpilers – slebetman Commented Dec 19, 2017 at 8:15
  • @slebetman: Quite. Like any other language feature, you need to be aware of what can and can't be polyfilled or transformed if transpiling for older engines. – T.J. Crowder Commented Dec 19, 2017 at 8:17
Add a ment  | 

1 Answer 1

Reset to default 7

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). 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):

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).

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.


* "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 ≤ 253-1.

本文标签: javascriptAny modern way to disable sort in Objectassign if objects has numeric keysStack Overflow