2

I would like the years to still be displayed desc order. In my request, the years and correctly sorted.

My PHP controller returns a json like this:

{  
   "2018":[  
      {  
         "id":25071,

      },
      {  
         "id":35037,
      }
   ],
   "2017":[  
      {  
         "id":20449,
      },
      {  
         "id":25797
      }
   ],
   "2016":[  
      {  
         "id":20419,
      },
      {  
         "id":25778
      }  
   ]
}

But when I get in in my vue component

getYears: function(){
    axios.get('/blablabla/').then(
        response => {
            console.log(response.data);
            this.years=response.data;
        }
    );
}

I get this result

{2008: Array(1), 2010: Array(5), 2011: Array(7), 2012: Array(7), 2013: Array(4), 2014: Array(5), 2015: Array(8), 2016: Array(4), 2017: Array(6), 2018: Array(6)} 2008: (...) 2010: (...) 2011: (...) 2012: (...) 2013: (...) 2014: (...) 2015: (...) 2016: (...) 2017: (...) 2018: (...)

How can I prevent it to change the order?

Thanks a lot!

Maxiss
  • 986
  • 1
  • 16
  • 35
  • 1
    That's weird, do you have any sorting on client side? Maybe in some axios global config? – Tarasovych May 23 '19 at 12:26
  • If you require a specific order, perhaps use an array? – Jonnix May 23 '19 at 12:28
  • The object is convenient beaucause then I can just display it in the table. But I didn't add any specific configuration, no – Maxiss May 23 '19 at 12:34
  • Well actually, adding ' ' (a blank space) after the year work. (casting it to a string doesn't though). Strange, but I guess I can just use this... :) – Maxiss May 23 '19 at 12:36
  • if this is a mongodb request you can project the sort in the route by appending `.sort({year : -1})` to the query statement .. assuming `year` is the name of the document holding the years – Len Joseph May 23 '19 at 13:01

1 Answers1

3

Thats not an axios problem. In ES2015 non-integer keys where returned in insertion order. Browser things. You made it into a string, so its not an integer anymore.

If you want to read more about it: click here

liqSTAR
  • 1,225
  • 1
  • 12
  • 22