我有一个ajax调用,它返回一个非常复杂的JSON对象,我很难对它进行排序。
I have an ajax call that returns a JSON object that is pretty complex and I'm having a hard time sorting it.
我的电话:
$.post('/reports-ajax',arguments, function(data) {}回复:
{ "10001":{ "unitname":"Fort Worth", "discounts":{"12-02-2012":"34.810000","12-03-2012":"20.810000","12-04-2012":"27.040000"}, "gross":{"12-02-2012":"56.730000","12-03-2012":"19.350000","12-04-2012":"66.390000"}, "net":{"12-02-2012":"61.920000","12-03-2012":"98.540000","12-04-2012":"39.350000"}, "discounts_total":82.66, "gross_total":82.47, "net_total":99.81, "number":10001 }, "10002":{ "unitname":"Dallast", "discounts":{"12-02-2012":"12.600000","12-03-2012":"25.780000","12-04-2012":"47.780000","12-05-2012":"45.210000"}, "gross":{"12-02-2012":"29.370000","12-03-2012":"91.110000","12-04-2012":"60.890000","12-05-2012":"51.870000"}, "net":{"12-02-2012":"16.770000","12-03-2012":"65.330000","12-04-2012":"13.110000","12-05-2012":"06.660000"}, "discounts_total":131.37, "gross_total":33.24, "net_total":101.87, "number":10002 }, "32402":{ "unitname":"Austin", "discounts":{"12-05-2012":"52.890000","12-02-2012":"22.430000","12-03-2012":"58.420000","12-04-2012":"53.130000"}, "gross":{"12-05-2012":"25.020000","12-02-2012":"2836.010000","12-03-2012":"54.740000","12-04-2012":"45.330000"}, "net":{"12-04-2012":"92.200000","12-05-2012":"72.130000","12-02-2012":"13.580000","12-03-2012":"96.320000"}, "discounts_total":186.87, "gross_total":161.1, "net_total":174.23, "number":32402 } }我过去了每个调用都有一个标准函数,并用highcharts做一些很棒的东西,但现在我试图通过net_total调用对响应进行排序,我无法弄明白。
I go over the function with a standard each call and do some awesome stuff with highcharts but now I'm trying to sort the responses by the net_total call and I can't figure it out.
我试过 .sort()并且错误地说它不是一个函数。我已经阅读了一段时间,但我猜我找不到合适的结果。这看起来很有希望:对一组JavaScript对象进行排序但它失败的 .sort 不是一个函数。似乎大多数 .sort 在[]数组上而不是完整的对象..
I tried .sort() and it errors out that its not a function. I've been reading for a while but guess I'm not finding the right results. This looked promising: Sorting an array of JavaScript objects but it failed with the .sort is not a function. It seems most .sort are on [] arrays not full objects..
任何帮助都将不胜感激。
Any help would be greatly appreciated.
推荐答案排序对象没有意义,因为对象键没有位置值。例如,这个:
Sorting objects doesn't make sense since object keys have no positional value. For example, this:
{ a:1, b:2 }这个:
{ b:2, a:1 }是完全相同的对象。它们不仅仅是相似的,它们是相同的。
are exactly the same object. They're not just similar, they're the same.
javascript本身没有任何东西给对象键任何位置值。有些人可能错误地认为:
Nothing in javascript per se gives object keys any positional value. Some people perhaps are mistaken in the belief that:
for (var key in obj) {以特定顺序迭代对象键。但这是错误的。你应该总是假设循环中的以随机顺序处理对象密钥,始终是。
iterates through the object keys in a specific sequence. But this is wrong. You should always assume that the for .. in loop processes object keys in random order, always, all the time.
显然,如果您要编写Web浏览器,则不会实现随机数生成器来解析循环中的 for ..因此,大多数Web浏览器对于循环中的循环处理对象键的方式具有意外稳定性。
Obviously, if you're going to write a web browser, you're not going to implement a random number generator to parse a for .. in loop. Therefore most web browsers have an accidental stability to how the for .. in loop processes object keys.
通过浏览浏览器来学习javascript的开发人员可能会发现他们的浏览器按字母顺序迭代对象,或者按键添加到对象的顺序。但这完全是偶然的,不能依赖。浏览器供应商可能在将来更改此行为,而不会违反任何向后兼容性(除了由相信对象具有排序顺序的人编写的错误脚本)。更不用说不同的浏览器具有不同的javascript实现,因此不一定具有相同的对象内部键排序。
Developers who learn javascript by playing around with the browser may figure out that their browser iterates through objects in alphabetical order for example, or the order the keys were added to the object. But this is totally accidental and cannot be relied upon. The browser vendor may change this behavior in the future without violating any backwards compatability (except with buggy scripts written by people who believe objects have a sort order). Not to mention that different browsers have different implementations of javascript and therefore not necessarily have the same internal key ordering of objects.
以上所有内容除此之外。 密钥排序顺序在javascript中没有任何意义,观察到的任何行为仅仅是实现细节。简而言之,javascript对象没有密钥顺序,只是假设它是随机的。
All the above is besides the point. "Key sort order" does not make any sense in javascript and any behavior observed is merely implementation detail. In short, javascript object does not have key order, just assume it's random.
解决方案
现在,您真正要做的是不对对象进行排序(您不能,它没有意义) 。您真正想要做的是按特定顺序处理对象属性。解决方案是简单地创建一个对象键的数组(具有排序顺序),然后使用该数组处理该对象:
Now, what you're really trying to do is not sort the object (you can't, it doesn't make sense). What you're really trying to do is process the object attributes in a specific order. The solution is to simply create an array (which has sorting order) of object keys and then process the object using that array:
// First create the array of keys/net_total so that we can sort it: var sort_array = []; for (var key in Response) { sort_array.push({key:key,net_total:Response[key]_total}); } // Now sort it: sort_array.sort(function(x,y){return x_total - y_total}); // Now process that object with it: for (var i=0;i<sort_array.length;i++) { var item = Response[sort_array[i].key]; // now do stuff with each item }更多推荐
如何通过嵌套值对JSON对象进行排序?
发布评论