3

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 ?

dcas
  • 73
  • 1
  • 7
  • 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. – CertainPerformance Jun 02 '18 at 06: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?](https://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order) – Jonathan Lonowski Jun 02 '18 at 06: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 – Isaac Jun 02 '18 at 06:10

2 Answers2

1

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.

Dishonered
  • 6,544
  • 7
  • 23
  • 43
  • No guarantee about order reference [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries) it says `same order as that provided by a for...in loop` and [for ... in documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in) 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 – HMR Jun 02 '18 at 07:41
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);
Ankit Agarwal
  • 28,439
  • 5
  • 29
  • 55
  • `JSON object` No such thing. – CertainPerformance Jun 02 '18 at 06:07
  • 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 Jun 02 '18 at 06:10
  • @CertainPerformance you should read this https://www.w3schools.com/js/js_json_objects.asp – Ankit Agarwal Jun 02 '18 at 06:10
  • 1
    @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*. – Gerardo Furtado Jun 02 '18 at 06:13
  • @GerardoFurtado cool guys I understood what you meant. Edited – Ankit Agarwal Jun 02 '18 at 06:13
  • w3schools has always been a pretty poor reference, and this case just gives more credence to that idea - they're wrong again. [There's no such thing as a "JSON Object"](http://benalman.com/news/2010/03/theres-no-such-thing-as-a-json/) – CertainPerformance Jun 02 '18 at 06:13
  • @AnkitAgarwal Yes, sometimes it scares me how wrong the W3C is. – Matus Dubrava Jun 02 '18 at 06:14
  • 1
    @MatusDubrava The problem is not W3C, but w3schools, which is something entirely different, a mediocre tutorial site unrelated to the World Wide Web Consortium. – CertainPerformance Jun 02 '18 at 06:16
  • @CertainPerformance Of course, I should have at least a coffee before trying to do something :) – Matus Dubrava Jun 02 '18 at 06:18
  • Cool guys. These things really matters – Ankit Agarwal Jun 02 '18 at 06:21
  • Do you have a reference that Object.entries orders in alphabetical key order? According to MDN [here](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries) it says `same order as that provided by a for...in loop` and [for ... in documentation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in) 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 – HMR Jun 02 '18 at 07:36
  • @HMR I haven't mentioned that `Object.entries` orders in alphabetical key order. I have said `Further clarification on sorting the object by ascending values of key when they are numeric. The javascript sorts them behind the scenes.` – Ankit Agarwal Jun 02 '18 at 07:37
  • My English not so good but `when you console.log the object Object.entries(object3) you get that sorted` would suggest guarantee of sorted array when using Object.entries. This may be the case now but the standard says **arbitrary order** – HMR Jun 02 '18 at 07:39