jQuery的:$的。每个排序 [英] jQuery: Sort results of $.each
问题描述
唯一的例子我能找到使用 $。每个
是HTML样的人,这不是我想要的。我有以下对象:
VAR OBJ = {
OBJ1:39,
obj2的:6,
obj3:文本
OBJ4:文本
obj5:0
};
通过像这样的对象,我循环:
$(阵列)。每个(函数(指数值){
// ...
});
我想通过 obj3
和 OBJ4
进行排序。 preferrably不使用异步方法,我怎么能之前(或期间)输出的结果进行排序? (我也不会通过这要循环两次,因为可能有数百个在任何给定的时间。
VAR阵列= {
OBJ1:39,
obj2的:6,
obj3:文本
OBJ4:文本
obj5:0
};
不是数组(它的名称虽然)。它是一个对象。通过 obj3
排序的想法和 OBJ4
并没有真正意义。
现在,如果你对这个对象转换为对象的数组,你可以进行排序与中的Array.sort
方法阵列。
VAR阵列= [
{OBJ1:39,
obj2的:6,
obj3:文本
OBJ4:文本
obj5:0
},{OBJ1:40,
obj2的:7,
obj3:文本2
OBJ4:文字3
obj5:0
}
];中的Array.sort(功能(A,B){ 变种TEXTA = a.obj3.toLowerCase();
变种TEXTB = b.obj3.toLowerCase(); 如果(TEXTA< TEXTB)
返回-1;
如果(TEXTA> TEXTB)
返回1;
返回0;
});
和课程,由数字财产进行排序的,它会仅仅是:
中的Array.sort(功能(A,B){
返回a.obj1 - b.obj1;
});
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:
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.
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.
Now, if you were to convert this object to an array of objects, you could sort that array with the array.sort
method.
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;
});
这篇关于jQuery的:$的。每个排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!