0

Check this example

var an_obj1 = { 100: 'x', 2: 'x', 7: 'x' };
console.log(Object.keys(an_obj1)); // console: ['2', '7', '100']

var an_obj2 = { 'z': 'x', 'a': 'x', 'b': 'x' };
console.log(Object.keys(an_obj2)); // console: ['z', 'a', 'b']

How can I make a result like ['100', '2', '7'] (original order)? Off course, I can't change keys, becouse are from backend.

Regards...

pablorsk
  • 2,420
  • 1
  • 25
  • 26
  • 5
    Object properties have no order. If you need to maintain the order of properties, you have to change your datastructure. Maybe something like `[{ name: 100, value: 'x' }, ...]` – Sirko Jul 06 '16 at 15:51
  • store the object key within an another array with expected order – Pranav C Balan Jul 06 '16 at 15:53
  • With javascript object arrays that your have key and value pairs every browser has its own order. So if you do a loop on your object array on every browser you will most probably get different results in different browsers. So this is how I resolved this issue when I had it. You can make your array object as a two-dimensional array. – Cengiz Araz Jul 06 '16 at 15:54
  • I'm thinking about this with a solution like this: `var an_obj1 = { '_100': 'x', '_2': 'x', '_7': 'x' };`. On chrome, this preserve the original order. I need test on onther browsers? – pablorsk Jul 06 '16 at 17:10

0 Answers0