1

I use this code to collect data() but its not working in chrome because it come in reversed order, how can I make the result match.

This is the same code. I just get different resutls across chrome/firefox which make it not functioning in chrome.

/* get checked filters */
strJson = '{';
jQuery('.uag-filter-option').each( function() {
    if (jQuery(this).hasClass('checked')) {
        jQuery.each(jQuery(this).data(), function(i, v) {
            strJson += i + ":'" + v + "',";
        });
    }
});
strJson = strJson.slice(0, -1);
strJson += '}';

alert(strJson);

In FireFox I get:

{sort:\'sortbydate\',method:\'sortby\'}

in Chrome I get:

{method:\'sortby\',sort:\'sortbydate\'}

How can I get the same result in Chrome as in Firefox? I assume it is how the data() was handled in different browsers?

Here is the anchor containing the data:

<a href="#" class="uag-filter-option" data-method="sortby" data-sort="sortbydate">date</a>

Any thoughts please?

It seems that:

When accessing data attribues using jQuery.data(), the order of them is reversed in Firefox and IE. How to come around that to make it match in all browsers?

Ahmed Fouad
  • 2,683
  • 10
  • 27
  • 54
  • 2
    You shouldn't have to worry about keys order of json object because you should retrieve value depending of key – A. Wolff May 16 '13 at 15:47
  • 1
    So you are getting the same data just the elements are in a different order? If so, and it matters, just sort the resulting object keys. – km6zla May 16 '13 at 15:48
  • Why does it matter the order if you aren't accessing the object values by an index number? `obj.method` is the same either way. – sbeliv01 May 16 '13 at 15:48
  • 1
    If the order matters, use an array. Objects don't have a guaranteed sort order. – Kevin B May 16 '13 at 15:48
  • Well, I need to, It stops my filtering, it does not work anymore. in chrome to be specific. it only works when it is ordered as in firefox. – Ahmed Fouad May 16 '13 at 15:48
  • Yes order matters. The php filtering stops working in Chrome order. :( – Ahmed Fouad May 16 '13 at 15:48
  • Use this function to sort the data array by key name first: http://stackoverflow.com/a/12834464/146602 – phirschybar May 16 '13 at 15:49
  • Run the json through a sort function – karthikr May 16 '13 at 15:50
  • Sample would help. thanks – Ahmed Fouad May 16 '13 at 15:50
  • If order matters, you're using the wrong container for your data, as objects explicitly do not guarantee a certain order. And building the string that way seems like a really weird approach ? – adeneo May 16 '13 at 15:52
  • 1
    Why are you building your own JSON? This is usually a bad idea! Why not just use `JSON.stringify($('.uag-filter-option').data())`? – Rocket Hazmat May 16 '13 at 15:52
  • Why not store the data as json in a single data-attribute? – Kevin B May 16 '13 at 15:57
  • Does not help! My main issue is that I need the key (The first one) they must be in order in all browsers. If someeone has solution to make them all look like the same one in fiefox that will solve my issue. – Ahmed Fouad May 16 '13 at 16:03

2 Answers2

2

You really shouldn't care what order the properties are in. You also should use JSON.stringify() instead of manually serializing the result:

strJson = JSON.stringify(jQuery('.uag-filter-option.checked').data());
Brandon
  • 34,233
  • 7
  • 73
  • 82
  • Does not help! My main issue is that I need the key (The first one) they must be in order in all browsers. If someeone has solution to make them all look like the same one in fiefox that will solve my issue. – Ahmed Fouad May 16 '13 at 16:02
0

what about putting the fetched data in an array instead of a string and sort it, say, alphabetically? then build your string out of the array, no?

Georges Brisset
  • 226
  • 2
  • 6