0

I have a JSON sent to the front-end after converting it from LinkedHashMap. When I receive it using jQuery and iterate it through using $.each() my insertion order is changed.

Suppose the object after iterating (using .each()) is {'1':'pqr','2':'abc'} whereas the original insertion order on the servlet and when I get the JSON response is {'2':'abc'...} (by value).

How would I preserve the order in each? I have read that array does it but having a hard time writing anything meaningful.

Code is like:

$.ajax({
  type: "GET",
  url: "Servlet",
  dataType: "json",
  success: function(data)
            {
                $.each(data, function(key, value){
                        
                      // append to option element
                       });    
            }          
});
Or Assayag
  • 3,141
  • 8
  • 30
  • 52
Ford Perfect
  • 527
  • 1
  • 10
  • 26
  • 2
    JavaScript [does not guarantee the order of keys in an object][1]. If you need guaranteed order, use an array. [1]: http://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order – Adam Oct 20 '13 at 19:52
  • Are you able to change the JSON structure returned? – Mathletics Oct 20 '13 at 19:53

0 Answers0