5

I have checked this page : mozilla documentation

I dont understand why the index 0 with :

const object3 = { 100: 'a', 2: 'b', 7: 'c' };
console.log(Object.entries(object3)[0]);

// expected output: Array ["100", "a"] <== i thought of this

instead the documentation says you get :

// expected output: Array ["2", "b"]

Someone can explain it why ?

3
  • 1
    Per the spec, property names are not reliably ordered. They often happen to be in modern JS interpreters, but you can't count on it. In this case, possibly the property of 100 is seen last because 100 is greater than 2 and 7. Commented Jun 2, 2018 at 6:05
  • 1
    Objects are unordered. Or, they aren't required to retain a consistent or predictable order of their properties. The engine only has to guarantee that, in a given loop, each property is only visited once. – Does JavaScript Guarantee Object Property Order? Commented Jun 2, 2018 at 6:06
  • According to the doc, you can try to use for..in loop to console.log it to know the sequence. However, orders of key in objects are not guaranteed Commented Jun 2, 2018 at 6:10

2 Answers 2

2

The Docs say that Object.entries returns an array of given objects enumerable property [key,value] pairs . So yes its confusing if you look at this statement

const object3 = { 100: 'a', 2: 'b', 7: 'c' };

and end up getting ["2", "b"] when you call Object.entries(object3)[0].

When you are doing this Object.entries(object3)[0] , you are accessing a pair at the index of 0 returned by this function Object.entries(object) . The order of this array has nothing to do with how you defined the object3 in the first place. The order according to the doc is the same as the provided by a for...in loop. I ran the for...in loop on the object and this is what i got as the order.

2,7,100.

This is why you are getting ["2", "b"] instead of ["100", "a"]. As others have mentioned here , the order seems to be that way because 2<7<100.

Sign up to request clarification or add additional context in comments.

1 Comment

No guarantee about order reference here it says same order as that provided by a for...in loop and for ... in documentation states: A for...in loop iterates over the properties of an object in an arbitrary order So there is no guarantee about the order of for ... in, Object.keys, and Object.entries
0

This is because the object has numeric keys and when you manipulate the object with the numeric keys the javascript sort the key-values in ascending order of the key value and since you have keys 2, 7, and 100. So, check this when you console.log the object Object.entries(object3) you get that sorted and when you access [0] you get Array ["2", "b"]

const object3 = { 100: 'a', 2: 'b', 7: 'c' };
console.log(Object.entries(object3));

Further clarification on sorting the object by ascending values of key when they are numeric. The javascript sorts them behind the scenes.

var a = {
  10: 'ten',
  5: 'five',
  11: 'eleven',
  1: 'one'
};
console.log(a);

13 Comments

JSON object No such thing.
JSON is Javascript Object Notation, a way of formatting a string to represent an object. A string can be in JSON format, but an (unserialized) object is just an object.
@CertainPerformance you should read this w3schools.com/js/js_json_objects.asp
@AnkitAgarwal JSON is a data interchange format, which was based on JS (hence the J in JSON). There is no such a thing as JSON object.
@MatusDubrava The problem is not W3C, but w3schools, which is something entirely different, a mediocre tutorial site unrelated to the World Wide Web Consortium.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.