JavaScript Javascript Object [sorting] javascript object sorting function,javascript object sorting,js object sorting,

If i use integer as a key in javascript object it shorts automatically.

var obj = {"z": 100, "x": 75, "d": 116, "c": 15, "10":123, "9":12}
console.log(obj)

Output:

{9: 12, 10: 123, z: 100, x: 75, d: 116, c: 15}

It is not maintaining sequence for integer, It works for alphabetical strings.

My question is Why it is so ? and how to overcome this if i need to manage sequence?

Answer:1

Object key ordering is not guaranteed. See Does JavaScript Guarantee Object Property Order?. Use an array of objects to guarantee ordering.

var arr = [{"z":100}, {"x":75}, {"d":116}, {"c":15}, {"10":123}, {"9":12}]
Answer:2

Use an array if you want to keep the order. That should be the only way to maintain the order in javascript.

Answer:3

If you want to maintain the order use "9.0", "10.0" instead of 9 or 10.

Try with this

{"z":100,"x":75,"d":116,"c":15,"10.0":123,"9.0":12}

And it is a known issue issue 164

Answer:4

The order of properties in objects are not guaranteed in JavaScript. If you would like something similar to an object that does guarantee order look into the Map Object.

var myMap = new Map();
myMap.set('z', 100);
myMap.set('x', 75);
myMap.set('d', 116);
myMap.set('c', 15);
myMap.set('10',113);
myMap.set('9', 12);

Order in maps IS guaranteed

enter image description here

Arrays also keep order, yet are not keyed by names, rather by an index.

Answer:5

I have this regexp ^((?!['"])[\x00-\x7F])*$ and it matches the following string no its all right lag I ran it through RegexBuddy debugger and it seems that the check of negative lookahed (?!['"]) ...

I have this regexp ^((?!['"])[\x00-\x7F])*$ and it matches the following string no its all right lag I ran it through RegexBuddy debugger and it seems that the check of negative lookahed (?!['"]) ...

How to add class="active" to the clicked thumbnail image? HTML : <ul class="product-thumbnail"> <li> <img class="thumbnail" src="Nile_1_16_0095.jpg"> </li> ...

How to add class="active" to the clicked thumbnail image? HTML : <ul class="product-thumbnail"> <li> <img class="thumbnail" src="Nile_1_16_0095.jpg"> </li> ...

  1. jquery only clicked element
  2. jquery toggle only clicked element
  3. jquery select only clicked element
  4. jquery onclick only works once
  5. jquery onclick only once

I'm trying to add additional function to the Gtranslate module in Drupal to make it as a language switch block. When click the gtranslate block, the url will also redirect to the desired language ...

I'm trying to add additional function to the Gtranslate module in Drupal to make it as a language switch block. When click the gtranslate block, the url will also redirect to the desired language ...

Using the Javascript, Vue.js and Typeahead.js way shown by Jeffery Way and Algolia docs to index json objects from the Algolia platform. Currently when i search for the result i require and hit the ...

Using the Javascript, Vue.js and Typeahead.js way shown by Jeffery Way and Algolia docs to index json objects from the Algolia platform. Currently when i search for the result i require and hit the ...