你如何阻止 Chrome 和 Opera 按索引 ASC 对 JSON 对象进行排序? [英] How do you stop Chrome and Opera sorting JSON objects by Index ASC?

查看:31
本文介绍了你如何阻止 Chrome 和 Opera 按索引 ASC 对 JSON 对象进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了问题.

我使用 ajax 发送了一个正确格式的 JSON 对象:

 $.ajax({类型:POST",url: SITE_URL+'/data.php',数据类型:json",数据:{阿贾克斯:1},成功:功能(数据){控制台日志(数据);}});

然而,Opera 和 Chrome 虽然接收到相同的对象,但打印出的对象顺序不正确,似乎它们都按 ID 编号进行排序,而不是单独放置它!

有没有办法停止这种自动排序?

编辑,在发现它是按索引号排序后,我认为最好的方法可能是不使用索引来存储 object_id,而是存储我想要对对象进行排序的 id 号.

但是我还是想知道是否有办法停止排序.

谢谢

Edit2,我想指出的是,我将采用不同的方式来做这件事,因为我觉得我在用这种方法滥用对象.但是,我仍然想了解为什么 Opera 和 Chrome 认为更改对象 ID 的顺序是他们的权利:

问题在于我试图节省处理能力,假设我们有人拥有 ID,

1.John、2.Frank 和 3.Sally.然而,这些人中的每一个都有一个高属性集(和其他东西).1.John.180, 2.Frank.220, 3.Sally.150.为了节省处理时间,我要求人们按身高对结果进行排序,这样我就得到了一个包含 2、1、3 和其他属性的数组.我对这个数组进行 JSON 处理并将其发送到浏览器.

现在FF将保留新订单People[1] 仍然是 John,但在 For n as person 循环中,他们会乱序.

如果我无法解决这个问题,我就不必在 SQL 阶段费心排序,并在 JS 阶段添加额外的循环和排序到数组中,尽管我想避免浏览器承受更多压力,因为它已经一个 Js 重磅页面.

非常感谢

解决方案

不同的浏览器以不同的方式处理对象,我的错误是尝试使用我构建对象的顺序作为我不应该的参考.

I've got a problem.

Using ajax I sent a correctly formed JSON object using:

            $.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!

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.

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:

The problem would be me trying to save processing power, lets say we have people with an 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.

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.

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.

Many thanks

解决方案

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.

这篇关于你如何阻止 Chrome 和 Opera 按索引 ASC 对 JSON 对象进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆