Reputation: 8016
I was under impression that ES6 has introduced new features to get object properties in creation order using methods like Object.getOwnPropertyNames()
, Reflect.ownKeys
etc but while working on a problem I realised for negative keys the order is not maintained.
const object1 = {
"-1": 'somestring',
"3": 42,
"2": false,
"-3": true
};
console.log(Object.getOwnPropertyNames(object1));
// expected output: Array ["-1", "3, "2", "-3"]
// actual output: ["2", "3", "-1", "-3"]
console.log(Object.keys(object1));
What would be the correct way to get keys in creation order for such scenario?
EDIT: I dont want to sorted order, looking for creation order
Upvotes: 1
Views: 1989
Reputation: 1760
This is will, in case you don't want to change the type of the keys and keep them as strings.
Object.getOwnPropertyNames(object1).sort()
Upvotes: 0