Javascript object always being created in growing order

Asked

Viewed 73 times

3

I’m dealing with a similar code in a project:

var groupedObject = {};
groupedObject['2016'] = {};
groupedObject['2022'] = {};
groupedObject['2014'] = {};
groupedObject['2021'] = {};
console.log(groupedObject);

No matter the order in which the object values are created, the log ALWAYS returns the object in ascending order

Output:

2014:{}
2016:{}
2021:{}
2022:{}

I don’t want this behavior. Could anyone explain why? How to return the object in the order it is being created?

  • have to use an array

  • @Tomásantunes, Unfortunately in my situation I need it to be an object. And I need you to respect the order in which it is being created

3 answers

3

The order of insertion in the object is not guaranteed by the specification.

However, if you use a Map this is already true. This provides you the same functionality ensuring the insertion order, but the syntax will already be different.

The creation becomes:

var groupedObject = new Map();

To assign a value to a key you must use the method set passing the key and the value:

groupedObject.set('2016', {});

To get the value of a key you must use the get indicating the name of the key:

let valor = groupedObject.get('2016')

Scroll through all elements is done with for ... of:

for (let obj of groupedObject){
    //fazer algo com obj
}

See your example in Map:

var groupedObject = new Map();
groupedObject.set('2016', {});
groupedObject.set('2022', {});
groupedObject.set('2014', {});
groupedObject.set('2021', {});

for (let obj of groupedObject){
  console.log(obj);
}

console.log(groupedObject.get('2016'));

  • I thought of the same solution. Note that Map is not supported in older browsers (IE < 11), and partially supported in IE 11.

  • @bfavaretto I appreciate the comment, and yes it is indeed an important caveat.

2


When the names of Keys are purely numerical, they are automatically organized in ascending order, no matter what order you entered them in. If they are not numerical, the order will be maintained according to the inserted order.

I couldn’t understand the importance of that order, since it doesn’t matter of "2016" be before "2014" if when you want to access a key by name, she’ll be there (?).

But if you really want to sort by the insertion order, I suggest adding at least 1 letter to the Keys. I do not think that this would be detrimental to its implementation.

It would be something like:

var groupedObject = {};
groupedObject['n2016'] = {};
groupedObject['n2022'] = {};
groupedObject['n2014'] = {};
groupedObject['n2021'] = {};
console.log(groupedObject);

Or you could use a underscore:

var groupedObject = {};
groupedObject['2016_'] = {};
groupedObject['2022_'] = {};
groupedObject['2014_'] = {};
groupedObject['2021_'] = {};
console.log(groupedObject);

  • All the answers helped me to come up with a solution, but I believe this is the one that best suits my situation. Thank you!

1

The problem is not in the console.log Yes in the JS object that by definition does not maintain order, I found an alternative in Soen that can help you get the result you want.

var groupedObject = {};
groupedObject[' ' +2016] = {};
groupedObject[' ' +2022] = {};
groupedObject[' ' +2014] = {};
groupedObject[' ' +2021] = {};
console.log(groupedObject);

ref: How to Prevent Automatic Sort of Object Numeric Property?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.