Javascript jQuery:对 $.each 的结果进行排序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8886494/
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
jQuery: Sort results of $.each
提问by Brian Graham
The only examples I have been able to find of people using $.each
are html samples, and it's not what I want. I have the following object:
我能够找到的人们使用的唯一示例$.each
是 html 示例,这不是我想要的。我有以下对象:
var obj = {
obj1: 39,
obj2: 6,
obj3: 'text'
obj4: 'text'
obj5: 0
};
I loop through the object like so:
我像这样循环遍历对象:
$(array).each(function(index, value) {
// ...
});
I want to sort by obj3
and obj4
. Preferrably not using an asynchronous method, how can I sort the results before (or during) output? (I also don't want to loop through this twice, as there could be hundreds at any given time.
我想按obj3
和排序obj4
。最好不使用异步方法,如何在输出之前(或期间)对结果进行排序?(我也不想循环两次,因为在任何给定时间都可能有数百个。
回答by Adam Rackis
var array = {
obj1: 39,
obj2: 6,
obj3: 'text'
obj4: 'text'
obj5: 0
};
is not an array (its name notwithstanding). It is an object. The idea of sorting by obj3
and obj4
doesn't really make sense.
不是数组(尽管有它的名字)。它是一个对象。按obj3
和排序的想法obj4
并没有真正意义。
Now, if you were to convert this object to an array of objects, you could sort that array with the array.sort
method.
现在,如果要将此对象转换为对象数组,则可以使用该array.sort
方法对该数组进行排序。
var array = [
{ obj1: 39,
obj2: 6,
obj3: 'text'
obj4: 'text'
obj5: 0
},{ obj1: 40,
obj2: 7,
obj3: 'text2'
obj4: 'text3'
obj5: 0
}
];
array.sort(function(a, b) {
var textA = a.obj3.toLowerCase();
var textB = b.obj3.toLowerCase();
if (textA < textB)
return -1;
if (textA > textB)
return 1;
return 0;
});
and of course to sort by a numeric property, it'd simply be:
当然,要按数字属性排序,它只是:
array.sort(function(a, b) {
return a.obj1 - b.obj1;
});
回答by jfriend00
Object properties do not have a defined order. They cannot be sorted. Arrays do have order. If you want the keys in a specific order, you will have to put them in an array and define the order.
对象属性没有定义的顺序。它们无法排序。数组确实有顺序。如果您想要特定顺序的键,则必须将它们放在一个数组中并定义顺序。
You could grab all the property names (e.g. the keys) from the object, sort them and then iterate the properties in that order if you want. To do that, you'd do it like this:
您可以从对象中获取所有属性名称(例如键),对它们进行排序,然后根据需要按该顺序迭代属性。要做到这一点,你会这样做:
var obj = {
obj1: 39,
obj2: 6,
obj3: 'text'
obj4: 'text'
obj5: 0
};
var keys = [];
for (var prop in obj) {
keys.push(prop);
}
keys.sort();
for (var i = 0; i < keys.length; i++) {
var key = keys[i];
var value = obj[key];
// do whatever you want to do with key and value
}
As you will see, this requires an extra iteration to obtain and sort the list of keys. I'm not aware of any way around that. Obtaining the keys can be done in a modern browser with obj.keys()
, but internally that's probably an iteration through the object properties anyway and you'd need a shim to allow that to work in older browsers.
正如您将看到的,这需要额外的迭代来获取和排序键列表。我不知道有什么办法解决这个问题。可以在现代浏览器中使用 获取密钥obj.keys()
,但在内部,无论如何这可能是通过对象属性的迭代,并且您需要一个垫片来允许它在旧浏览器中工作。