jQuery 如何停止 Chrome 和 Opera 按索引 ASC 对 JSON 对象进行排序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5020699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you stop Chrome and Opera sorting JSON objects by Index ASC?
提问by Dorjan
I've got a problem.
我有问题。
Using ajax I sent a correctly formed JSON object using:
使用 ajax,我使用以下命令发送了一个正确格式的 JSON 对象:
$.ajax({
type: "POST",
url: SITE_URL+'/data.php',
dataType: "json",
data: { ajax: 1 },
success: function(data) {
console.log(data);
}
});
However, Opera and Chrome, although receiving the same object, print out the object in an incorrect order, it seems like they both perform a sort by ID number instead of just leaving it alone!
然而,Opera 和 Chrome 虽然接收到相同的对象,但以错误的顺序打印出对象,似乎它们都按 ID 编号执行排序,而不是单独放置它!
Is there a way to stop this auto sort?
有没有办法停止这种自动排序?
Edit, after finding out it is a sort by index number I'm thinking the best method might be to not use the index for storing the object_id and instead store the id number which I want to order the object by.
编辑,在发现它是按索引号排序后,我认为最好的方法可能是不使用索引来存储 object_id,而是存储我想要对对象进行排序的 id 号。
However I would still like to know if there is a way to stop the sort.
但是我仍然想知道是否有办法停止排序。
Thank you
谢谢
Edit2, I'll just like to note that I'm going to work on a different way of doing this, as I feel like I'm abusing objects with this method. However I'd still like to understand why Opera and Chrome feel it is their right to change the order of my objects IDs:
Edit2,我只是想指出我将采用不同的方式来做这件事,因为我觉得我在用这种方法滥用对象。但是,我仍然想了解为什么 Opera 和 Chrome 认为更改对象 ID 的顺序是他们的权利:
The problem would be me trying to save processing power, lets say we have people with an ID,
问题是我试图节省处理能力,假设我们有一个 ID,
1.John, 2.Frank and 3.Sally. However each of these people have a hight property set (and other things). 1.John.180, 2.Frank.220, 3.Sally.150. To save on processing, my I request the result of people be sorted by their height so I get an array of 2, 1, 3 with their other properties. I JSON this array and send it to the browser.
1.约翰,2.弗兰克和 3.莎莉。然而,这些人中的每一个都有一个高属性集(和其他东西)。1.John.180, 2.Frank.220, 3.Sally.150。为了节省处理时间,我要求人们按身高对结果进行排序,这样我就得到了一个包含 2、1、3 和其他属性的数组。我 JSON 这个数组并将其发送到浏览器。
Now FF will keep the new order People[1] would still be John but in a For n as person loop they'll be out of order.
现在 FF 将保持新的顺序 People[1] 仍然是 John,但在 For n as person 循环中,他们将失序。
If I can't get around this I'll just have to not bother sorting at the SQL stage and add extra looping and sorting into an array in the JS stage although I wanted to avoid more stress on the browser as its already a Js heavy page.
如果我无法解决这个问题,我就不必费心在 SQL 阶段进行排序,并在 JS 阶段添加额外的循环和排序到数组中,尽管我想避免给浏览器带来更多压力,因为它已经是一个沉重的 Js页。
Many thanks
非常感谢
采纳答案by Dorjan
Different browsers handle objects in different ways, my fault was to try and use the order I built an object as a reference where I shouldn't.
不同的浏览器以不同的方式处理对象,我的错是尝试使用我构建对象的顺序作为我不应该的参考。
回答by FarnhamBee
Had same problem, followed dmc's solution but just added a space in front of the int value to make it a string.
有同样的问题,遵循 dmc 的解决方案,但只是在 int 值前面添加了一个空格以使其成为字符串。
The advantage of using a space rather than another non numeric character is that the subsequently POSTed value can be used directly in a mySQL search clause without having to remove it again.
使用空格而不是另一个非数字字符的优点是可以直接在 mySQL 搜索子句中使用随后的 POST 值,而不必再次删除它。
回答by Michail
Changing integer to string didn't work for me (Chrome, jQuery 1.7.1). So to keep the order (yes, it's object abusing), I changed this:
将整数更改为字符串对我不起作用(Chrome,jQuery 1.7.1)。所以为了保持顺序(是的,这是滥用对象),我改变了这个:
optionValues0 = {"4321": "option 1", "1234": "option 2"};
to this
对此
optionValues0 = {"1": {id: "4321", value: "option 1"}, "2": {id: "1234", value: "option 2"}};
回答by James Long
Unless that JSON is an array, rather than an object, there is no standard that says it has to be in a certain order. However, this shouldn't be a problem since you don't need to iterate through the object to get the data, you can simply refer to the property.
除非 JSON 是一个数组,而不是一个对象,否则没有标准说它必须按特定顺序排列。但是,这应该不是问题,因为您不需要遍历对象来获取数据,您只需引用属性即可。
回答by dmc
Some browsers will sort keys which are int, on the other hand it's very easy to change that to string and later revert, mine solution was to add "i" to key, that made a trick. Works perfectly for each browsers :) Hope that helps :)
一些浏览器会对 int 类型的键进行排序,另一方面,很容易将其更改为字符串,然后再还原,我的解决方案是将“i”添加到键中,这是一个技巧。适用于每个浏览器:) 希望有所帮助:)
回答by Alasjo
I had the same "problem" and did not want to go back and change too much in the code. Figured out that at least Google Chrome only re-sorts numeric indexes. As long as the index is considered a string it will show up in the "intended" order. Could someone please verify this on Opera?
我有同样的“问题”,不想回去对代码进行太多更改。发现至少谷歌浏览器只对数字索引进行重新排序。只要索引被认为是一个字符串,它就会以“预期”的顺序显示。有人可以在 Opera 上验证一下吗?
回答by user3167654
None of the solutions worked for me (Aug 2017). Even if I used a string Chrome and Safari (didnt test other browsers) were still sorting based on the string. What I did instead was concatenate my integer key with value string. e.g
没有一个解决方案对我有用(2017 年 8 月)。即使我使用了字符串 Chrome 和 Safari(没有测试其他浏览器)仍然基于字符串进行排序。我所做的是将我的整数键与值字符串连接起来。例如
result[string_value + str(integer_key)] = string_value
This way the string is first and the browser is free to sort. Then I used some simple logic to separate the key from the value.
这样字符串就在前面,浏览器可以自由排序。然后我使用一些简单的逻辑将键与值分开。
Hope this helps.
希望这可以帮助。
回答by DynamicDan
You need to trick Google Chrome (and others) into thinking you have a string and not a number.
Changing the JSON to {"2423":'abc', "2555":'xyz'}
will not work.
你需要欺骗谷歌浏览器(和其他人)认为你有一个字符串而不是一个数字。将 JSON 更改为{"2423":'abc', "2555":'xyz'}
将不起作用。
I had to use "_2423" and "_2555" as indexes in my object to get it to work.
我必须使用“_2423”和“_2555”作为对象中的索引才能使其工作。
Further, debugging the output to console.log or using for x in y
gave different results as to _.each
method (see the underscore framework).
此外,将输出调试到 console.log 或 usingfor x in y
给出了不同的结果_.each
(参见下划线框架)。
You can see my test/proof in JSFiddle with "_"
vs no prefix and the for x in y
loop: http://jsfiddle.net/3f9jugtg/1/
您可以在"_"
没有前缀和for x in y
循环的JSFiddle 中看到我的测试/证明:http: //jsfiddle.net/3f9jugtg/1/
回答by user1039177
I resolved this problem using this code:
我使用以下代码解决了这个问题:
$('.edit_district').editable("/moderator/sale/edit_sale/", {
data : " {'_7':'10 street', '_9':'9 street', '_11':'park'}",
type : 'select',
submit : 'OK',
onblur : 'submit'
});
and in script.php:
在 script.php 中:
...
case "id_district":
$district = Districts::GetDistrictByID(Database::getInstance(), (int)substr($value,1));
if($district instanceof District){
$data["id_district"] = $district->id_district;
echo $district->title;
}
break;
...
回答by Collector
Seems the best way is to avoid associative arrays at all. When you want to send an associate array simply send it as two separate arrays - one of keys and one of values. Here's the PHP code to do that:
似乎最好的方法是完全避免关联数组。当您想发送关联数组时,只需将其作为两个单独的数组发送 - 一个键和一个值。这是执行此操作的 PHP 代码:
$arWrapper = array();
$arWrapper['k'] = array_keys($arChoices);
$arWrapper['v'] = array_values($arChoices);
$json = json_encode($arWrapper);
and the simple JavaScript code to do whatever you'd like with it
和简单的 JavaScript 代码来做任何你想做的事情
for (i=0; i < data['k'].length; i++) {
console.log('key:' + data['k'][i] + ' val:' + data['v'][i]);
}
I had a similar issue and posted on jQuery: $.getJSON sorting the data on Chrome / IE?
我有一个类似的问题并发布在jQuery 上:$.getJSON 在 Chrome/IE 上对数据进行排序?