0

I have a very simple array which I am just doing json encode by php and JSON.parse by Jquery,

Array
(
    [193] => Array
        (
            [2607] => Array
                (
                    [type] => demoaccount
                    [gid] => 193
                    [name] => demo Name here1
                    [description] => test description
                    [order] => 0
                    [ispopular] => 0
                )
            [1003] => Array
                (
                    [type] => demoaccount
                    [gid] => 193
                    [name] => demo Name here2
                    [description] =>  test description
                    [order] => 1
                    [ispopular] => 1
                )
            [1006] => Array
                (
                    [type] => demoaccount                    
                    [name] => demo Name here3
                    [description] =>  test description                    
                    [order] => 2
                    [ispopular] => 0
                )
        )
)

After Parse with Json Array key sequence gets change to Asending

{ '1003':
   { type: 'demoaccount',
     gid: '193',
     name: 'demo Name here2',
     description: 'test description',
     order: '1',
     ispopular: '1' },
  '1006':
   { type: 'demoaccount',
     gid: '193',
     name: 'demo Name here3',
     description: 'test description',     
     order: '2',
     ispopular: 0 },
  '2607':
   { type: 'demoaccount',
     gid: '193',
     name: 'demo Name here1',
     description: 'test description',     
     order: 0,
     ispopular: 0 } }

Array Key gets rearrange from 2607,1003,1006 to 1003,1006,2607

I want to Maintain sequnce as it is,

How can I achieve this using jQuery?

Azhar
  • 11
  • 3
  • [Does JavaScript Guarantee Object Property Order?](https://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order) – Andreas Jan 03 '20 at 10:41
  • If you need them to be in a specific order then add that order as an additional property to your response (`"sort-order": [ 2607, 1003, 1006]`) and use this property to iterate over the parsed object. – Andreas Jan 03 '20 at 10:43
  • I have already passed the Order element in which i passed the sequence but after parse i have to sort the array again, I am using this in node js and facing other challenges to sort the array. is it possible to Mainatian the same sequence some how? – Azhar Jan 03 '20 at 10:47
  • Numeric properties will be ordered in ascending order (for the relevant spec see the linked question in my first comment). You cannot change this behavior. As already said, if you need a specific order, you have to define (and use) it: https://stackoverflow.com/questions/5467129/sort-javascript-object-by-key – Andreas Jan 03 '20 at 10:51
  • I am getting below error on sorting array UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): TypeError: unordered.sort is not a function – Azhar Jan 03 '20 at 12:54
  • That's a separate question which definitely requires a [mcve]. – Andreas Jan 03 '20 at 15:04
  • Does this answer your question? [Does JavaScript Guarantee Object Property Order?](https://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order) – Andreas Jan 03 '20 at 15:04

0 Answers0