如何对对象的JS对象进行排序? [英] How to sort a JS object of objects?

查看:786
本文介绍了如何对对象的JS对象进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在PHP中构建了一个对象,使用了JSON_encode函数并通过ajax将其作为JSON字符串发送到我的JS脚本。然后我将它转换回一个对象。我遇到的问题是我想保持对象按照它最初创建的顺序。请看一下这个对象在进入JS之后的样子:

I have built an object in PHP, used JSON_encode function and send it as a JSON string to my JS script via ajax. Then I convert it back to an object. The problem I am having is that I wanted to keep the object in the order that it was originally created in. Please see this picture of what the object looks like once I get it into JS:

当我创建对象时,它按字母顺序按客户字段排序。以A开头的客户名称将首先出现,B秒等等。正如您所看到的,现在,对象的第一个元素是以S开头的客户。看起来它以某种方式按照顶层的键自动排序对象,这是一个整数,所以我理解为什么会这样。

When I created the object, it was sorted by the customer field alphabetically. The customer name starting with A would come first, B second, etc. As you can see, now, the first element of the object as customer starting with S. It looks like somehow it got automatically sorted by the key of the top-level object, which is an integer, so I understand why this happened.

所以我想做的是重新排序这个对象,以便所有子对象按客户按字母顺序排列。这可能吗?如果是这样,我该怎么做?

So i want to do is re-sort this object so that all the sub-objects are sorted by the customer field alphabetically. Is this possible? If so, how do I do it?

谢谢!

推荐答案

这可能是JavaScript对象和JavaScript数组之间的区别。对象更像是哈希表,其中键没有按任何特定顺序排序,而数组是值的线性集合。

It's probably the difference between a JavaScript Object and a JavaScript Array. Objects are more like hash tables, where the keys aren't sorted in any particular order, whereas Arrays are linear collections of values.

在你的后端,请确保你'重新编码数组,而不是对象。检查最终编码的JSON,如果您的对象集合被{}而不是[]包围,则它被编码为对象而不是数组。

In your back end, make sure you're encoding an array, rather than an object. Check the final encoded JSON, and if your collection of objects is surrounded by {} instead of [], it's being encoded as an object instead of an array.

您可以遇到问题,因为看起来你正试图通过ID号访问对象,这就是你希望那些对象在最终数组中占用的索引,这会带来另一个问题,因为你可能不想要一个数组当你只存储少量的值时,有40,000个条目。

You may run into a problem since it looks like you're trying to access the objects by an ID number, and that's the index you want those objects to occupy in the final array, which presents another problem, because you probably don't want an array with 40,000 entries when you're only storing a small amount of values.

如果你只想迭代对象,你应该确保编码一个数组而不是一个对象。如果要按特定ID访问对象,则可能必须对客户端对象进行排序(即从JSON响应中获取对象,然后创建另一个数组并将这些对象排序到其中,这样您就可以已排序的对象仍然可以通过id)访问它们。

If you just want to iterate through the objects, you should make sure you're encoding an array instead of an object. If you want to access the objects by specific ID, you'll probably have to sort the objects client-side (i.e. have the object from the JSON response, and then create another array and sort those objects into it, so you can have the sorted objects and still be able to access them by id).

您可以通过Google轻松找到有效的排序算法(或使用以下ELCas中的算法)。

You can find efficient sorting algorithms (or use the one below from ELCas) easily via Google.

这篇关于如何对对象的JS对象进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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